请帮忙,这个模拟器无效:
class ClassBeingTested {
private AnotherClass anotherClass;
public void someMethod() {
int ans = anotherClass.targetMethod(5);
// Use ans here
}
}
//我的测试
ClassBeingTested classObject;
AnotherClass anotherClassObject;
@Before
public void setup() {
// Initialize anotherClassObject here
classObject = new ClassBeingTested(anotherClassObject);
new NonStrictExpectations(anotherClassObject) {{
invoke(anotherClassObject, "targetMethod", Integer.class); result = 100;
}};
}
@Test
public void testSomeMethod() {
classObject.someMethod();
}
答案 0 :(得分:0)
当我用实际预期的int值替换Integer.class时,模拟工作。
new NonStrictExpectations(anotherClassObject) {{
invoke(anotherClassObject, "targetMethod", 5); result = 100;
}};
答案 1 :(得分:0)
这种使用JMockit进行模拟的方式怎么样?
import mockit.Injectable;
import mockit.Tested;
...
@Tested
ClassBeingTested classObject;
@Injectable
AnotherClass anotherClassObject;
@Before
public void setup() {
new Expectations() {{
anotherClassObject.targetMethod(anyInt); result = 100;
}};
}
@Test
public void testSomeMethod() {
classObject.someMethod();
}