我正在使用Roboelectric进行测试,并且我试图实现一个必须检查静态方法是否已被调用的测试。我的测试看起来像这样:
@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
@PrepareForTest({DeviceUtils.class})
@SmallTest
public class ContactsListPresenterTest extends BasePresenterTest {
...
@Test
public void onCallContactClicked_nullArgument_methodNotCalled() {
PowerMockito.mockStatic(DeviceUtils.class);
presenter.onCallNotaryClicked(context, null);
PowerMockito.verifyStatic(Mockito.never());
DeviceUtils.dialPhoneNumber(context, Mockito.anyString());
}
@Test
public void onCallContactClicked_nullArgument_methodCalled() {
PowerMockito.mockStatic(DeviceUtils.class);
presenter.onCallNotaryClicked(context, new Contact());
PowerMockito.verifyStatic(Mockito.times(1));
DeviceUtils.dialPhoneNumber(context, Mockito.anyString());
}
}
但是测试失败并出现以下错误:
org.powermock.api.mockito.ClassNotPreparedException:
The class x.DeviceUtils not prepared for test.
To prepare this class, add class to the '@PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or method level.
我做错了什么?我已经添加了@PrepareForTest注释,我想这都是因为我使用的是RobolectricTestRunner而不是PowerMockitoRunner,但我需要RoboelectricTestRunner用于同一类别的其他测试。
非常感谢
答案 0 :(得分:1)
您使用的是PowerMockRule
吗?如Robolectric docs中所述