任何()与任何(class.class)oikito
我无法理解为什么下面的两个测试没有给出相同的结果。
@Service
public class SomeManager{
private final SomeDependency someDependency;
@Autowired
public SomeManager(SomeDependency someDependency){
this.someDependency = someDependency;
}
public List<returnType> methodToTest(Arg arg){
List<JsonObject> jo = someDependency.search(arg);
return jo.stream().map(returnType::parse).collect(Collectors.toList());
}
}
用ANY()
测试。这个测试通行证。
@RunWith(SpringJUnit4ClassRunner.class)
public class TestMethodToTest(){
@Test
public void TestMethod(){
SomeDependency someDependency = mock(SomeDependency.class);
List<JsonObject> expected := \some valid list of JsonObject\
// Here I used any() method.
when(someDependency.search(any())).thenReturn(expected);
SomeManager someManager = new SomeManager(someDependency);
List<returnType> actual = someManager.methodToTest(any(Arg.class));
assertArrayEquals(acutal.toArray(), expected.stream().map(returnType::parse).toArray());
}
}
但是由于search(arg)
有一天的方法
采用类的参数arg
这样更改了上述测试:
@RunWith(SpringJUnit4ClassRunner.class)
public class TestMethodToTest(){
@Test
public void TestMethod(){
SomeDependency someDependency = mock(SomeDependency.class);
List<JsonObject> expected := \some valid list of JsonObject\
// Here I used any(Arg.class) method.
when(someDependency.search(any(Arg.class))).thenReturn(expected);
SomeManager someManager = new SomeManager(someDependency);
List<returnType> actual = someManager.methodToTest(any(Arg.class));
assertArrayEquals(acutal.toArray(), expected.stream().map(returnType::parse).toArray());
}
}
第二个测试失败,输出失败java.lang.assertionError:数组长度有所不同,预期。长度= 1 actent.length = 0
。这是什么可能的原因?
Note :“值greenge.length = 1
”中的输出取决于用户作为测试中JSON对象的有效列表提供的值。
I am not able to understand why below two tests are not giving the same result.
@Service
public class SomeManager{
private final SomeDependency someDependency;
@Autowired
public SomeManager(SomeDependency someDependency){
this.someDependency = someDependency;
}
public List<returnType> methodToTest(Arg arg){
List<JsonObject> jo = someDependency.search(arg);
return jo.stream().map(returnType::parse).collect(Collectors.toList());
}
}
Test with any()
. This Test pass.
@RunWith(SpringJUnit4ClassRunner.class)
public class TestMethodToTest(){
@Test
public void TestMethod(){
SomeDependency someDependency = mock(SomeDependency.class);
List<JsonObject> expected := \some valid list of JsonObject\
// Here I used any() method.
when(someDependency.search(any())).thenReturn(expected);
SomeManager someManager = new SomeManager(someDependency);
List<returnType> actual = someManager.methodToTest(any(Arg.class));
assertArrayEquals(acutal.toArray(), expected.stream().map(returnType::parse).toArray());
}
}
But since search(Arg arg)
method of SomeDependency
takes parameter of class Arg
so I changed above test like this:
@RunWith(SpringJUnit4ClassRunner.class)
public class TestMethodToTest(){
@Test
public void TestMethod(){
SomeDependency someDependency = mock(SomeDependency.class);
List<JsonObject> expected := \some valid list of JsonObject\
// Here I used any(Arg.class) method.
when(someDependency.search(any(Arg.class))).thenReturn(expected);
SomeManager someManager = new SomeManager(someDependency);
List<returnType> actual = someManager.methodToTest(any(Arg.class));
assertArrayEquals(acutal.toArray(), expected.stream().map(returnType::parse).toArray());
}
}
This second test fails with output java.lang.AssertionError: array lengths differed, expected.length=1 actual.length=0
.What's the possible reason behind this?
Note: The value expected.length=1
in output depends on what value is provided by the user as valid list of json objects in the test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
差异源于
任何
与NULL匹配的事实,而AnyClass
与NULL不匹配。参见任何(class&lt; t&gt; type)
与给定类型的任何对象匹配,不包括nulls。您将null传递到正在测试的方法:
Any()
返回您将其传递给测试方法的null。请注意,以这种方式使用参数匹配器是非法的 - 当 and verify 时,您只能将其在
中拨打
。您应该将
arg
的真实实例传递给正在测试的方法。参见
The difference stems from the fact that
any
matches null, whileanyClass
does not match null. See ArgumentMatchers javadoc:any()
Matches anything, including nulls and varargs.any(Class<T> type)
Matches any object of given type, excluding nulls.You are passing null to your method under test here:
any()
returns null which you pass to method under test.Note that using argument matchers this way is illegal - you should only call them inside calls to
when
andverify
. You should pass a real instance ofArg
to method under test.See Mockito javadoc
对于那些使用Kotlin的人,请尝试
任何&lt; class&lt;*&gt;
For those using Kotlin try
any<Class<*>>