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

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

      1. 如何模拟 @InjectMocks 类的方法?

        时间:2023-10-01

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

            • <small id='bGhPl'></small><noframes id='bGhPl'>

              <legend id='bGhPl'><style id='bGhPl'><dir id='bGhPl'><q id='bGhPl'></q></dir></style></legend>
            • <tfoot id='bGhPl'></tfoot>

                <tbody id='bGhPl'></tbody>

                  本文介绍了如何模拟 @InjectMocks 类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  例如我有处理程序:

                  @Component
                  public class MyHandler {
                  
                    @AutoWired
                    private MyDependency myDependency;
                  
                    public int someMethod() {
                      ...
                      return anotherMethod();
                    }
                  
                    public int anotherMethod() {...}
                  }
                  

                  为了测试它,我想写这样的东西:

                  to testing it I want to write something like this:

                  @RunWith(MockitoJUnitRunner.class}
                  class MyHandlerTest {
                  
                    @InjectMocks
                    private MyHandler myHandler;
                  
                    @Mock
                    private MyDependency myDependency;
                  
                    @Test
                    public void testSomeMethod() {
                      when(myHandler.anotherMethod()).thenReturn(1);
                      assertEquals(myHandler.someMethod() == 1);
                    }
                  }
                  

                  但每当我尝试模拟它时,它实际上都会调用 anotherMethod() .我应该用 myHandler 做什么来模拟它的方法?

                  But it actually calls anotherMethod() whenever I try to mock it. What should I do with myHandler to mock its methods?

                  推荐答案

                  首先mock MyHandler方法的原因可能如下:我们已经测试过anotherMethod(),它的逻辑很复杂,那么,如果我们可以verify 它正在调用,为什么我们需要再次测试它(就像 someMethod() 的一部分)?
                  我们可以通过:

                  First of all the reason for mocking MyHandler methods can be the following: we already test anotherMethod() and it has complex logic, so why do we need to test it again (like a part of someMethod()) if we can just verify that it's calling?
                  We can do it through:

                  @RunWith(MockitoJUnitRunner.class)
                  class MyHandlerTest {
                  
                    @Spy  
                    @InjectMocks  
                    private MyHandler myHandler;  
                  
                    @Mock  
                    private MyDependency myDependency;  
                  
                    @Test  
                    public void testSomeMethod() {  
                      doReturn(1).when(myHandler).anotherMethod();  
                      assertEquals(myHandler.someMethod() == 1);  
                      verify(myHandler, times(1)).anotherMethod();  
                    }  
                  }  
                  

                  注意:在 'spying' 对象的情况下,我们需要使用 doReturn 而不是 thenReturn(小解释是 这里)

                  Note: in case of 'spying' object we need to use doReturn instead of thenReturn(little explanation is here)

                  这篇关于如何模拟 @InjectMocks 类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PowerMock + Mockito VS Mockito 单独 下一篇:Mockito 匹配器和基元数组

                  相关文章

                  最新文章

                  <tfoot id='ReQ8p'></tfoot>

                    <bdo id='ReQ8p'></bdo><ul id='ReQ8p'></ul>
                  1. <legend id='ReQ8p'><style id='ReQ8p'><dir id='ReQ8p'><q id='ReQ8p'></q></dir></style></legend>

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