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

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

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

        Mockito:在使用@InjectMocks 时模拟被测试方法调用的

        时间:2023-09-25

          <bdo id='0zgqo'></bdo><ul id='0zgqo'></ul>

          <small id='0zgqo'></small><noframes id='0zgqo'>

              <tbody id='0zgqo'></tbody>

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

              <legend id='0zgqo'><style id='0zgqo'><dir id='0zgqo'><q id='0zgqo'></q></dir></style></legend>
                • 本文介绍了Mockito:在使用@InjectMocks 时模拟被测试方法调用的同一类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个要测试的类,它有几个外部依赖项和几个内部方法.我想为 MethodA 写一个测试,但是 not 有 Method A 对 MethodB 的内部调用来实际执行 MethodB.我想模拟/存根 MethodB 并返回特定的东西.通常我会使用 when/thenReturn 但它的行为不像我预期的那样 - 它实际上在创建模拟本身时跳转到方法 B.

                  I have a class I want to test that has several external dependencies, and a couple internal methods. I would like to write a test for MethodA, but not have Method A's internal call to MethodB to actually exercise MethodB. I'd like to mock/stub MethodB and return something specific instead. Usually I'd use when/thenReturn but it doesn't behave like I expect - it actually jumps into Method B while creating the mock itself.

                  MyService.java

                  @Service
                  public class MyService {
                  
                    @Autowired
                    private ServiceA serviceA;
                  
                    @Autowired
                    private ServiceB serviceB;
                  
                      public SomeObject methodA() {
                        // some logic using serviceA.method and serviceB.method that creates "output"
                        SomeObject someObject = methodB(output);
                        return someObject;
                      }
                  
                      public SomeObject methodB(SomeObject someObject) {
                        // deep mysteries done here to someObject
                        return someObject 
                      }
                  }
                  

                  MyServiceTest.java

                  public class MyServiceTest {
                  
                    @Mock
                    private ServiceA serviceA;
                  
                    @Mock
                    private ServiceB serviceB;
                  
                    @InjectMocks
                    private MyService myService;
                  
                    @Before
                    public void setUp() throws Exception {
                      MockitoAnnotations.initMocks(this);
                    }
                  
                    @Test
                    public void methodATest() {
                      when(serviceA.method()).thenReturn(stuff);
                      when(serviceB.method()).thenReturn(otherStuff);
                  
                      // here is what I would like to do
                      when(myService.methodB()).thenReturn(mockedSomeObject); //<- doesn't work
                  
                      assertThat(myService.methodA().getSomeObjectProperty())
                          .isEqualTo("property");
                    }
                  }
                  

                  我查看了使用 Mockito.mock(MyService.class) 手动模拟 MyService 类的解决方案,但是(因为上面的示例显然是人为的)我的实际的类有很多外部依赖项,我更喜欢一个解决方案,它仍然允许我使用 @Mock@Autowired 依赖项和 @ 模拟服务InitMocks 用于被测类,除非它根本不可能.

                  I've looked at solutions that manually mock the MyService class with Mockito.mock(MyService.class), but (as the above example is obviously contrived) my actual class has quite a few external dependencies and I'd prefer a solution that still allows me to mock the service using @Mock for the @Autowired dependencies and @InitMocks for the class under test, unless it's simply not possible.

                  我试过了:

                  Mockito.doReturn(mockedSomeObject).when(myService.methodB(any(SomeObject.class));
                  

                  但在为该方法创建模拟时也会进入 MethodB,这不应该发生.

                  but that also steps into MethodB when creating the mock for that method, which shouldn't be happening.

                  提前感谢您的帮助!

                  推荐答案

                  尝试将@Spy 添加到您的 InjectMocks 并使用该对象以稍微不同的语法预期"它们.

                  Try Adding @Spy to your InjectMocks and use the object to "expect" them in a slightly different syntax.

                  导入 org.mockito.Spy;

                   @InjectMocks
                   @Spy
                   private MyService myService; 
                  

                  现在模拟服务调用

                   Mockito.doReturn(mockedSomeObject).when(myService).methodB();
                  

                  还将其他模拟调用更改为此

                  Also change the other mock call to this

                  Mockito.doReturn(stuff).when(serviceA).method();
                  Mockito.doReturn(otherStuff).when(serviceB).method();
                  

                  这篇关于Mockito:在使用@InjectMocks 时模拟被测试方法调用的同一类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito - 文件的模拟行为 下一篇:每次测试都会重置 Mock 对象吗?

                  相关文章

                  最新文章

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

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

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

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