• <bdo id='mgcJm'></bdo><ul id='mgcJm'></ul>
    <i id='mgcJm'><tr id='mgcJm'><dt id='mgcJm'><q id='mgcJm'><span id='mgcJm'><b id='mgcJm'><form id='mgcJm'><ins id='mgcJm'></ins><ul id='mgcJm'></ul><sub id='mgcJm'></sub></form><legend id='mgcJm'></legend><bdo id='mgcJm'><pre id='mgcJm'><center id='mgcJm'></center></pre></bdo></b><th id='mgcJm'></th></span></q></dt></tr></i><div id='mgcJm'><tfoot id='mgcJm'></tfoot><dl id='mgcJm'><fieldset id='mgcJm'></fieldset></dl></div>
      <tfoot id='mgcJm'></tfoot>
      1. <legend id='mgcJm'><style id='mgcJm'><dir id='mgcJm'><q id='mgcJm'></q></dir></style></legend>

        <small id='mgcJm'></small><noframes id='mgcJm'>

        Mockito:通缉但未调用

        时间:2023-10-01

                <tbody id='ogdRc'></tbody>
              <tfoot id='ogdRc'></tfoot>

                <bdo id='ogdRc'></bdo><ul id='ogdRc'></ul>

                <i id='ogdRc'><tr id='ogdRc'><dt id='ogdRc'><q id='ogdRc'><span id='ogdRc'><b id='ogdRc'><form id='ogdRc'><ins id='ogdRc'></ins><ul id='ogdRc'></ul><sub id='ogdRc'></sub></form><legend id='ogdRc'></legend><bdo id='ogdRc'><pre id='ogdRc'><center id='ogdRc'></center></pre></bdo></b><th id='ogdRc'></th></span></q></dt></tr></i><div id='ogdRc'><tfoot id='ogdRc'></tfoot><dl id='ogdRc'><fieldset id='ogdRc'></fieldset></dl></div>

                <small id='ogdRc'></small><noframes id='ogdRc'>

                <legend id='ogdRc'><style id='ogdRc'><dir id='ogdRc'><q id='ogdRc'></q></dir></style></legend>
                  本文介绍了Mockito:通缉但未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有如下测试方法:

                  MyClass myClass= Mockito.mock(MyClass.class);
                  Mockito.when(myClass.methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class))).thenReturn(Collections.<X, Y> emptyMap());
                  
                  assertNull(myClass.methodToTest(myObject));
                  Mockito.verify(myClass).methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class));
                  

                  methodUsedInMethodBeingTested 是一种我想模拟并返回空映射的方法.但我收到失败消息说

                  The methodUsedInMethodBeingTested is a method that I want to mock and return an empty map. But I am getting the failure message saying

                  需要但未调用 myClass.methodUsedInMethodBeingTested()

                  Wanted but not invoked myClass.methodUsedInMethodBeingTested()

                  .

                  MyClass
                  {
                     public XYZ methodToTest()
                     {
                      ....
                      ....
                      Map<X,Y> mp = methodUsedInMethodBeingTested(myTypeParam);
                      .....
                     }
                  
                     public Map<X,Y> methodUsedInMethodBeingTested(MyTypeParam myTypeParam)
                     {
                      .....
                     }
                  }
                  

                  推荐答案

                  你误解了什么是 mock.当你在做的时候

                  You're misunderstanding what a mock is. When you're doing

                  MyClass myClass = Mockito.mock(MyClass.class);
                  // ...
                  assertNull(myClass.methodToTest(myObject));
                  

                  您实际上并没有在您的真实对象上调用 methodToTest.您正在模拟上调用 methodToTest,默认情况下,它什么也不做并返回 null,除非它被存根.引用 Mockito 文档:

                  You're not actually invoking methodToTest on your real object. You're invoking methodToTest on the mock, which by default, does nothing and return null, unless it was stubbed. Quoting from Mockito docs:

                  默认情况下,对于所有返回值的方法,mock 返回 null、一个空集合或适当的原始/原始包装值(例如:0、false、...对于 int/Integer、boolean/Boolean、...).

                  By default, for all methods that return value, mock returns null, an empty collection or appropriate primitive/primitive wrapper value (e.g: 0, false, ... for int/Integer, boolean/Boolean, ...).

                  这解释了您随后的错误:该方法实际上没有在模拟上调用.

                  This explains your subsequent error: the method was really not invoked on the mock.

                  看来你想要的是 spy 改为:

                  It seems what you want here is a spy instead:

                  您可以创建真实对象的间谍.当您使用 spy 时,会调用 real 方法(除非方法被存根).

                  You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).

                  警告说明:由于调用的是真正的方法,因此您不应该使用 Mockito.when 而是更喜欢 Mockito.doReturn(...).when,否则该方法将被真正调用一次.如果考虑表达式:

                  A note of warning though: since it is the real methods that are getting called, you should not use Mockito.when but prefer Mockito.doReturn(...).when, otherwise the method will be called once for real. If you consider the expression:

                  Mockito.when(myClass.methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class))).thenReturn(Collections.<X, Y> emptyMap());
                               ^-----------------------------------^
                                   this will be invoked by Java
                  

                  方法 when 的参数必须被计算,但这意味着方法 methodUsedInMethodBeingTested 将被调用.因为我们有一个间谍,所以它是真正的方法将被调用.所以,改为使用:

                  the argument of the method when must be evaluated, but this means the method methodUsedInMethodBeingTested will be invoked. And since we have a spy, it is the real method that will be invoked. So, instead, use:

                  MyClass spy = Mockito.spy(new MyClass());
                  Mockito.doReturn(Collections.<X, Y> emptyMap()).when(spy).methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class));
                  assertNull(spy.methodToTest(myObject));
                  Mockito.verify(spy).methodUsedInMethodBeingTested(Matchers.any(MyTypeParam.class));
                  

                  这篇关于Mockito:通缉但未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Android Jack mockito 替代品 下一篇:Spring Boot 测试中的 MockBean 注解导致 NoUniqueBeanDe

                  相关文章

                  最新文章

                  1. <tfoot id='xcVet'></tfoot>

                  2. <small id='xcVet'></small><noframes id='xcVet'>

                    <i id='xcVet'><tr id='xcVet'><dt id='xcVet'><q id='xcVet'><span id='xcVet'><b id='xcVet'><form id='xcVet'><ins id='xcVet'></ins><ul id='xcVet'></ul><sub id='xcVet'></sub></form><legend id='xcVet'></legend><bdo id='xcVet'><pre id='xcVet'><center id='xcVet'></center></pre></bdo></b><th id='xcVet'></th></span></q></dt></tr></i><div id='xcVet'><tfoot id='xcVet'></tfoot><dl id='xcVet'><fieldset id='xcVet'></fieldset></dl></div>
                  3. <legend id='xcVet'><style id='xcVet'><dir id='xcVet'><q id='xcVet'></q></dir></style></legend>
                        <bdo id='xcVet'></bdo><ul id='xcVet'></ul>