我是JMockit的新手。我如何在使用期望或任何更好的方法为 method1(( 编写测试时模拟 method2((。我用期望来模拟依赖类。
public class A {
Dependency dep = new Dependency();
public int method1(int val){
//some business logic
//....
//.....
dep.someMethod();
int ret = method2(val);
return ret;
}
public int method2(int val) {
//some business logic
//....
//.....
return val;
}
}
创建 method2 的模型并根据您测试的测试用例返回。
new MockUp<A>() {
@Mock
int method2(int val) // no access modifier required
{
return yourRequiredOutput;
}
}
new Expectations() {
{
depMock.someMethod();
}
};