次のようにテスト対象のユニット:
@Component(value = "UnitUnderTest")
public class UnitUnderTest {
@Resource(name = "propertiesManager")
private PropertiesManager prop;
public List<String> retrieveItems() {
List<String> list = new ArrayList<String>();
String basehome = prop.get("FileBase");
if (StringUtils.isBlank(basehome)) {
throw new NullPointerException("basehome must not be null or empty.");
}
File target = new File(basehome, "target");
String targetAbsPath = target.getAbsolutePath();
File[] files = FileUtils.FolderFinder(targetAbsPath, "test");//A utility that search all the directories under targetAbsPath, and the directory name mush match a prefix "test"
for (File file : files) {
list.add(file.getName());
}
return list;
}
}
以下のようなテストケース:
public class TestExample {
@Tested
UnitUnderTest unit;
@Injectable
PropertiesManager prop;
/**
*
*
*/
@Test
public void retrieveItems_test(@NonStrict final File target,@Mocked FileUtils util){
new Expectations(){
{
prop.get("FileBase");
result="home";
target.getAbsolutePath();
result="absolute";
FileUtils.FolderFinder("absolute", "test");
result=new File[]{new File("file1")};
}
};
List<String> retrieveItems = logic.retrieveItems();
assertSame(1, retrieveItems.size());
}
}
失敗しました。retrieveItems の実際の結果は空です。「FileUtils.FolderFinder(targetAbsPath, "test")」は常に空の File[] を返すことがわかりました。それは本当に奇妙です。
おそらく、ファイルインスタンスの「ターゲット」もモックしたためです。静的メソッド FileUtils.FolderFinder をモックするだけであれば問題なく動作します。
誰が問題が何であるか知っていますか?ここで必要なローカル変数インスタンスをモックすることは可能ですか? このターゲットインスタンスなど?
どうもありがとう!