我对JMockit的理解是,它将用模拟替换模拟对象的所有实例(除非你另有说明)。
因此,在实例化一个我试图模拟的对象后,我很困惑得到一个NPE 。
测试的目的不是调查导致 NPE 的对象,但我确实需要模拟它以执行测试,因为它执行一些数据库操作来验证某些输入。
我的测试代码是这样的(不是复制意大利面,因为它是工作代码,但应该突出显示问题):
public class ClassToTest{
public execute(){
MyDependency myDep = getDependency();
myDep.doSomething(); //I get a NPE here, implying getDependency returned null
}
protected MyDependency getDependency(){
return new MyDependency("an Arg", "another Arg");
}
}
我的测试方法:
@Test
public void testCreateHorseDogMeetingByCodeDataProviderTruncated()
throws IllegalArgumentException, SQLException,
IllegalCountryLocationCombo, MEPException {
// Arrange
ClassToTest myClass = new ClassToTest();
new NonStrictExpectations() {
MyDependency mockDep;
{
//Set up my expectations, not related to MyDependency
}
};
// Act
myClass.execute();
// Assert
new Verifications() {
{
//some verification stuff
}
};
}
谁能帮我解决这个 NPE 问题,以便我完成测试?