• <tfoot id='8dSYQ'></tfoot>
      <bdo id='8dSYQ'></bdo><ul id='8dSYQ'></ul>

    <small id='8dSYQ'></small><noframes id='8dSYQ'>

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

        <legend id='8dSYQ'><style id='8dSYQ'><dir id='8dSYQ'><q id='8dSYQ'></q></dir></style></legend>
      1. Mockito,@InjectMocks 最终字段的奇怪行为

        时间:2023-09-26
        <i id='Mhh36'><tr id='Mhh36'><dt id='Mhh36'><q id='Mhh36'><span id='Mhh36'><b id='Mhh36'><form id='Mhh36'><ins id='Mhh36'></ins><ul id='Mhh36'></ul><sub id='Mhh36'></sub></form><legend id='Mhh36'></legend><bdo id='Mhh36'><pre id='Mhh36'><center id='Mhh36'></center></pre></bdo></b><th id='Mhh36'></th></span></q></dt></tr></i><div id='Mhh36'><tfoot id='Mhh36'></tfoot><dl id='Mhh36'><fieldset id='Mhh36'></fieldset></dl></div>

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

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

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

                  本文介绍了Mockito,@InjectMocks 最终字段的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我看到我认为是错误的行为.@InjectMocks 似乎并没有在每个测试方法之前创建一个新的测试主题.@Mock 在哪里.在以下示例中,如果 Subject.section 是最后一个 @Test 失败.如果它不是最终的,则两者都通过.我目前的解决方法是使用@BeforeClass,但这并不理想.

                  I am seeing behaviour that I believe is a bug. @InjectMocks does not seem to create a new test subject before every test method. Where as @Mock does. In the following example, if Subject.section is final one @Test fails. If its not final both pass. My current workaround is to use @BeforeClass, but this is not ideal.

                  主题.java:

                  package inject_mocks_test;
                  public class Subject {
                  
                      private final Section section;
                  
                      public Subject(Section section) {
                          this.section = section;
                      }
                  
                      public Section getSection() {
                          return section;
                      }
                  }
                  

                  Section.java:

                  Section.java:

                  package inject_mocks_test;
                  
                  public class Section {}
                  

                  SubjectTest.java

                  SubjectTest.java

                  package inject_mocks_test;
                  
                  import org.mockito.InjectMocks;
                  import org.mockito.Mock;
                  import org.mockito.MockitoAnnotations;
                  import org.testng.annotations.BeforeMethod;
                  import org.testng.annotations.Test;
                  
                  import static org.testng.Assert.assertEquals;
                  
                  public class SubjectTest {
                  
                      @Mock
                      Section section;
                  
                      @InjectMocks
                      Subject subject;
                  
                      @BeforeMethod
                      public void setup() {
                          MockitoAnnotations.initMocks(this);
                      }
                  
                      @Test
                      public void test1() {
                          assertEquals(section, subject.getSection());
                      }
                  
                      @Test
                      public void test2() {
                          assertEquals(section, subject.getSection());        
                      }
                  }
                  

                  干杯.

                  推荐答案

                  您正在使用 @InjectMocks 进行 constructor 注入.只要 Mockito 找到未初始化的字段(空),这将起作用.JUnit 在每次测试之前都会创建一个新的测试类实例,所以 JUnit 粉丝(像我一样)永远不会遇到这样的问题.TestNg 没有创建测试类的新实例.它保持测试方法之间的状态,所以当 MockitoAnnotations.initMocks(this) 第二次调用时,Mockito 会发现 subject 字段已经初始化并尝试使用 <强>场注入.这在另一回合将一直有效,直到该领域不是最终的.

                  You are using the @InjectMocks for constructor incjection. This will work as long as Mockito finds the field not initalized (null). JUnit is creating a new instance of the test class before each test, so JUnit fans (like me) will never face such problem. TestNg is not creating a new instance of test class. It's keeping the state between test methods, so when MockitoAnnotations.initMocks(this) is called for the second time, Mockito will find subject field already initialized and will try to use field injection. This on the other turn will work until the field is not final.

                  这是一个错误吗?我相信不是——而是 API 设计的自然结果.一些解决方法是添加

                  Is this is a bug? I believe not - rather a natural consequence of the API design. Some workaround for you would be to add

                  this.subject = null;
                  

                  在一些 @AfterMethod 方法中.

                  这篇关于Mockito,@InjectMocks 最终字段的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在匿名类中测试方法时,如何使用 Powermockito 模拟 下一篇:Mockito、Java 9 和 java.lang.ClassNotFoundException:sun.ref

                  相关文章

                  最新文章

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

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

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

                  1. <tfoot id='s4Kjm'></tfoot><legend id='s4Kjm'><style id='s4Kjm'><dir id='s4Kjm'><q id='s4Kjm'></q></dir></style></legend>