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

      <tfoot id='Y9e2Z'></tfoot>
      • <bdo id='Y9e2Z'></bdo><ul id='Y9e2Z'></ul>

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

    3. 多个级别的@Mock 和@InjectMocks

      Multiple levels of @Mock and @InjectMocks(多个级别的@Mock 和@InjectMocks)
            <tfoot id='qGiAA'></tfoot>

              <tbody id='qGiAA'></tbody>

          • <legend id='qGiAA'><style id='qGiAA'><dir id='qGiAA'><q id='qGiAA'></q></dir></style></legend>
              <bdo id='qGiAA'></bdo><ul id='qGiAA'></ul>

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

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

                本文介绍了多个级别的@Mock 和@InjectMocks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                所以我知道在 Mockito 中,@InjectMocks 会通过 @Mock 的注解注入任何它可以注入的东西,但是如何处理这种情况呢?

                So I understand that in Mockito @InjectMocks will inject anything that it can with the annotation of @Mock, but how to handle this scenario?

                @Mock
                private MockObject1 mockObject1;
                
                @Mock
                private MockObject2 mockObject2;
                
                @InjectMocks
                private SystemUnderTest systemUnderTest = new SystemUnderTest();
                

                假设 MockObject2 有一个 MockObject1 类型的属性,而 SystemUnderTest 有一个 MockObject2 类型的属性.我想将 mockObject1 注入到 mockObject2 中,并将 mockObject2 注入到 systemUnderTest 中.

                Imagine that MockObject2 has an attribute that is of type MockObject1, and SystemUnderTest has an attribute of type MockObject2. I would like to have mockObject1 injected into mockObject2, and mockObject2 injected into systemUnderTest.

                这可以通过注释实现吗?

                Is this possible with annotations?

                推荐答案

                由于我在这里没有得到任何回复,所以我在 Mockito 论坛上提问.这是讨论的链接:https://groups.google.com/d/topic/mockito/hWwcI5UHFi0/讨论

                Since I didn't get any response here I asked on the Mockito forums. Here is a link to the discussion: https://groups.google.com/d/topic/mockito/hWwcI5UHFi0/discussion

                总结答案,从技术上讲,这会破坏嘲笑的目的.您实际上应该只模拟 SystemUnderTest 类所需的对象.在本身就是模拟的对象中模拟事物是毫无意义的.

                To summarize the answers, technically this would kind of defeat the purpose of mocking. You should really only mock the objects needed by the SystemUnderTest class. Mocking things within objects that are themselves mocks is kind of pointless.

                如果你真的想这样做,@spy 可以提供帮助

                If you really wanted to do it, @spy can help

                这篇关于多个级别的@Mock 和@InjectMocks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                How to convert Integer to int?(如何将整数转换为整数?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                Inconsistent behavior on java#39;s ==(java的行为不一致==)
                Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                  <i id='DfljF'><tr id='DfljF'><dt id='DfljF'><q id='DfljF'><span id='DfljF'><b id='DfljF'><form id='DfljF'><ins id='DfljF'></ins><ul id='DfljF'></ul><sub id='DfljF'></sub></form><legend id='DfljF'></legend><bdo id='DfljF'><pre id='DfljF'><center id='DfljF'></center></pre></bdo></b><th id='DfljF'></th></span></q></dt></tr></i><div id='DfljF'><tfoot id='DfljF'></tfoot><dl id='DfljF'><fieldset id='DfljF'></fieldset></dl></div>
                  1. <tfoot id='DfljF'></tfoot>

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

                          <tbody id='DfljF'></tbody>

                        <legend id='DfljF'><style id='DfljF'><dir id='DfljF'><q id='DfljF'></q></dir></style></legend>
                          <bdo id='DfljF'></bdo><ul id='DfljF'></ul>