Java 是否可以使用JMockit模拟局部变量?
试验装置如下所示:Java 是否可以使用JMockit模拟局部变量?,java,jmockit,Java,Jmockit,试验装置如下所示: @Component(value = "UnitUnderTest") public class UnitUnderTest { @Resource(name = "propertiesManager") private PropertiesManager prop; public List<String> retrieveItems() { List<String> list = new ArrayList
@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;
}
}
组件(value=“UnitUnderTest”)
公共类单元测试{
@资源(name=“propertiesManager”)
私人财产管理人财产;
公共列表检索项(){
列表=新的ArrayList();
String basehome=prop.get(“FileBase”);
if(StringUtils.isBlank(basehome)){
抛出新的NullPointerException(“basehome不能为null或空”);
}
文件目标=新文件(basehome,“目标”);
字符串targetAbsPath=target.getAbsolutePath();
File[]files=FileUtils.FolderFinder(targetBSPATH,“test”);//一个实用程序,用于搜索targetBSPATH下的所有目录,目录名必须与前缀“test”匹配
用于(文件:文件){
list.add(file.getName());
}
退货清单;
}
}
测试用例如下:
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());
}
}
公共类测试示例{
@测试
单元被测单元;
@注射的
物业管理公司;
/**
*
*
*/
@试验
public void retrieveItems_test(@NonStrict final File target,@mock FileUtils util){
新期望(){
{
prop.get(“FileBase”);
结果=“主页”;
target.getAbsolutePath();
结果=“绝对”;
FolderFinder(“绝对”、“测试”);
结果=新文件[]{new File(“file1”)};
}
};
List retrieveItems=logic.retrieveItems();
assertSame(1,retrieveItems.size());
}
}
它失败了。retrieveItems的实际Result为空。我发现“FileUtils.FolderFinder(targetBSPath,“test”)”总是返回一个空文件[]。真奇怪
这可能是因为我还模拟了文件实例“target”。如果我只模拟静态方法FileUtils.FolderFinder,它就可以正常工作
有人知道问题出在哪里吗?有可能像我在这里要求的那样模拟局部变量实例吗?比如这个目标实例
非常感谢 问题是我应该定义要模拟的方法
@Test
public void retrieveItems_test(@Mocked(methods={"getAbsolutePath"}) 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());
}
@测试
public void retrieveItems_test(@Mocked(methods={“getAbsolutePath”})最终文件目标,@Mocked FileUtils util){
新期望(){
{
prop.get(“FileBase”);
结果=“主页”;
target.getAbsolutePath();
结果=“绝对”;
FolderFinder(“绝对”、“测试”);
结果=新文件[]{new File(“file1”)};
}
};
List retrieveItems=logic.retrieveItems();
assertSame(1,retrieveItems.size());
}
这将很好。是的,以前整个
文件
类都是模拟的,所以像File.getName()
这样的调用也受到了影响。只模拟相关的方法(可以简化为@mock(“getAbsolutePath”)
)是有效的,但我建议不要模拟文件,因为在本测试中不需要它。感谢您的解释。正如你所说,这里真的没有必要模拟文件。^^,很高兴知道这一点。!