【发布时间】:2016-06-22 14:18:49
【问题描述】:
我在这里测试一个带有签名的 TestedClass 方法:
public static boolean getBoolean(ClassA classA);
这是我的模拟对象配置。
@Mock
private ClassA mockedCLass; //I try to mock ClassA behaviour
.....
when(mockedClass.getValues()).thenReturn(null,emptyList,oneElementList,defaultList);
getBoolean() 方法使用模拟对象。不幸的是,当我使用这种方法时,mock 的行为似乎完全错误。
boolean res1 = TestedClass.getBoolean(mockedClass);
boolean res2 = TestedClass.getBoolean(mockedClass);
boolean res3 = TestedClass.getBoolean(mockedClass);
boolean res4 = TestedClass.getBoolean(mockedClass);
但是,当我这样拆分时:
when(mockedClass.getValues()).thenReturn(null);
boolean res1 = TestedClass.getBoolean(mockedClass);
when(mockedClass.getValues()).thenReturn(emptyList);
boolean res2 = TestedClass.getBoolean(mockedClass);
等等,一切都很好。这里发生了什么?如果有任何帮助,我将不胜感激。
编辑: //测试用例
List<String> emptyList = Collections.<String>emptyList();
List<String> defaultList = Arrays.asList("one", "two", "three");
List<String> oneElementList = Arrays.asList("one");
这是来自TestedClass.的布尔方法示例
public static boolean getBoolean(ClassA classA){
return classA.getValues() == null || classA.getValues().size() <= 1; }
当我为第一种方法调用测试用例时,我得到:
1)null,defaultList,oneElementList -> true,true,true instead of true,false,true
2)oneElementList,null,defaultList -> error, Null pointer exception
3)null,oneElementList,defaultList,emptyList -> true,false,true,true instead of true,true,false,true
4)null or defaultList or oneElementList or emptyList -> works fine for one case
【问题讨论】:
标签: java unit-testing junit mocking mockito