0
我正在用Roboelectric实现测试,我试图实现一个必须检查静态方法是否已被调用的测试。我的测试看起来像这样:用Robolectric和PowerMockito嘲笑静态方法
@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用于其他同类测试。
非常感谢
如果我添加了@Rule public PowerMockRule规则= new PowerMockRule();那么类中的所有测试失败:java.lang.RuntimeException:java.lang.ClassNotFoundException:org.powermock.classloading.DeepCloner – FVod