我正在使用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在同一类中的其他测试。
Thanks to lot
您是否使用PowerMockRule
?正如robolelectric文档中提到的