@RunWith(PowerMockRunner.class)
@PrepareForTest({ConsoleLog.class})
public class AdContentDataUnitTest {
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(ConsoleLog.class);
}
@Test
public void adContentData_sendTrackingEvent_noUrl() throws Exception {
mAdContentData = spy(mAdContentData);
// PowerMockito.doNothing().when(ConsoleLog.class);
verifyStatic();
mAdContentData.sendTrackingEvent("event1");
//verifyStatic();
}
}
将调用sendTrackingEvent,并将调用ConsoleLog.v(String,String).我可以在调试中看到调用静态方法,但出现以下日志并且测试失败:
Wanted but not invoked com.example.logger.ConsoleLog.v(
"AdContentData","sendTrackingEvent: event event1 does not exist."
);
我尝试在相同的日志之后添加verifyStatic,如果我删除第一个验证,则不会检查任何内容.如果我模拟整个ConsoleLog类,则会在此处检测到错误未完成的存根:[…] PowerMockitoCore.doAnswer.
有谁知道如何正确地做到这一点?
最佳答案
Do anyone know how to do it properly?
是.根本不要这样做.
class Person {
private final int id;
Person() {
id = IdGenerator.gen();
}
}
class Person {
private final int id;
Person() {
id = generateId();
}
protected int generateId() {
return IdGenerator.gen();
}
}
final int id = 1;
Person person = new Person() {
@Override
protected int generateId() {
return id;
}
};
// test with person,knowing we control id