我正在嘲笑一个看起来像这样的方法,我想测试是否返回了一个类型为MyClass的非null对象:
public class DBClass {
MyClass myobj;
public MyClass mockingMethod(String x) {
....//some processsing for myobj
return myobj;
}
}
调用该方法的类如下所示:
public class TestClass {
DBClass db; //includes getters and setters
public MyClass mocker (String x) {
return db.mockingMethod(x);
}
}
我的测试类看起来像这样:
@Mocked DBClass;
@BeforeMethod
public void setUp() {
DBClass db = new DBClass();
MyClass myobj = new MyClass(a,b,c,...);//instantiate with parameterized constructor
....//more processing
}
@Test
public void testmockingMethod(){
new Expectations() {
{
db.mockingMethod("ABC");
result = myobj;
}
};
Assert.assertEquals(testobj.mocker("ABC"),myobj);
}
我得到结果,它说它期望myobj(实例化的对象)但是找到了null 我究竟做错了什么?