我使用PowerMockito来嘲笑私有方法。但相反被嘲笑它被称为。我需要测试一下调用privateMethod()的strangeMethod()。Mocked私有方法被称为而不是被嘲弄
这里是我的测试类:
public class ExampleService {
public String strangeMethod() {
privateMethod();
return "Done!";
}
private String privateMethod() {
throw new UnsupportedOperationException();
}
}
我的测试方法:
@Test
public void strangeMethodTest() throws Exception {
ExampleService exampleService = PowerMockito.spy(new ExampleService());
PowerMockito.when(exampleService, "privateMethod").thenReturn("");
exampleService.strangeMethod();
}
由于测试我得到UnsupportedOperationException异常的结果。这意味着,该privateMethod()被调用。
你确定你需要这个摆在首位?这看起来很肮脏,一种方法是私人的原因。 – Neijwiert
尝试使用'@PrepareForTest({ExampleService.class})'和'@RunWith(PowerMockRunner.class)'注释您的测试类' – troig