• <legend id='8jhkY'><style id='8jhkY'><dir id='8jhkY'><q id='8jhkY'></q></dir></style></legend>

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

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

      1. <tfoot id='8jhkY'></tfoot>

        多个级别的@Mock 和@InjectMocks

        时间:2023-10-01

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

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

                1. 本文介绍了多个级别的@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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito 将模拟注入 Spy 对象 下一篇:通过 mockito 创建一个模拟列表

                  相关文章

                  最新文章

                    <legend id='rm83o'><style id='rm83o'><dir id='rm83o'><q id='rm83o'></q></dir></style></legend>
                  1. <small id='rm83o'></small><noframes id='rm83o'>

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