我如何在正在测试的班级中使用 Mockito 模拟其他班级?
例如:
MyClass.java
class MyClass {
public boolean performAnything() {
AnythingPerformerClass clazz = new AnythingPerformerClass();
return clazz.doSomething();
}
}
AnyPerformerClass.java
class AnythingPerformerClass {
public boolean doSomething() {
//very very complex logic
return result;
}
}
并测试:
@Test
public void testPerformAnything() throws Exception {
MyClass clazz = new MyClass();
Assert.assertTrue(clazz.performAnything());
}
我可以恶搞AnythingPerformerClass
排除不必要的逻辑AnythingPerformerClass
吗?我可以覆盖doSomething()
简单返回的方法true
吗false
?
为什么我指定 Mockito,因为我需要它来使用 Robolectric 进行 Android 测试。