任何()与任何(class.class)oikito

发布于 2025-01-31 18:57:46 字数 2264 浏览 5 评论 0原文

我无法理解为什么下面的两个测试没有给出相同的结果。

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2025-02-07 18:57:46

差异源于任何与NULL匹配的事实,而AnyClass与NULL不匹配。参见

  • ” > Any()匹配任何内容,包括nulls和varargs。
  • 任何(class&lt; t&gt; type)与给定类型的任何对象匹配,不包括nulls。

您将null传递到正在测试的方法:

List<returnType> actual = someManager.methodToTest(any(Arg.class));

Any()返回您将其传递给测试方法的null。

请注意,以这种方式使用参数匹配器是非法的 - 当 and verify 时,您只能将其在中拨打。您应该将arg的真实实例传递给正在测试的方法。

参见

像任何()一样的匹配器方法,eq()不返回匹配器。在内部,他们在堆栈上记录了一个匹配器并返回虚拟值(通常为null)。该实现是由于Java编译器施加的静态类型安全性。结果是您不能在经过验证/固执方法之外使用任何(),eq()方法。

The difference stems from the fact that any matches null, while anyClass 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:

List<returnType> actual = someManager.methodToTest(any(Arg.class));

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 and verify. You should pass a real instance of Arg to method under test.

See Mockito javadoc

Matcher methods like any(), eq() do not return matchers. Internally, they record a matcher on a stack and return a dummy value (usually null). This implementation is due to static type safety imposed by the java compiler. The consequence is that you cannot use any(), eq() methods outside of verified/stubbed method.

那些过往 2025-02-07 18:57:46

对于那些使用Kotlin的人,请尝试任何&lt; class&lt;*&gt;

For those using Kotlin try any<Class<*>>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文