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

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

        如何模拟 @InjectMocks 类的方法?

        How can I mock methods of @InjectMocks class?(如何模拟 @InjectMocks 类的方法?)
          <tbody id='eYhWm'></tbody>
        <i id='eYhWm'><tr id='eYhWm'><dt id='eYhWm'><q id='eYhWm'><span id='eYhWm'><b id='eYhWm'><form id='eYhWm'><ins id='eYhWm'></ins><ul id='eYhWm'></ul><sub id='eYhWm'></sub></form><legend id='eYhWm'></legend><bdo id='eYhWm'><pre id='eYhWm'><center id='eYhWm'></center></pre></bdo></b><th id='eYhWm'></th></span></q></dt></tr></i><div id='eYhWm'><tfoot id='eYhWm'></tfoot><dl id='eYhWm'><fieldset id='eYhWm'></fieldset></dl></div>

          1. <small id='eYhWm'></small><noframes id='eYhWm'>

              <tfoot id='eYhWm'></tfoot>

              • <bdo id='eYhWm'></bdo><ul id='eYhWm'></ul>
              • <legend id='eYhWm'><style id='eYhWm'><dir id='eYhWm'><q id='eYhWm'></q></dir></style></legend>
                • 本文介绍了如何模拟 @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 类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

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

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

                    <tfoot id='I9ZyD'></tfoot>
                    <legend id='I9ZyD'><style id='I9ZyD'><dir id='I9ZyD'><q id='I9ZyD'></q></dir></style></legend>
                      <tbody id='I9ZyD'></tbody>

                      • <bdo id='I9ZyD'></bdo><ul id='I9ZyD'></ul>