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 の問題を解決するのを手伝ってくれる人はいますか?