【问题标题】:Showing Interactions with @MockBean Annotated Mock显示与 @MockBean 带注释的 Mock 的交互
【发布时间】:2017-07-03 10:05:45
【问题描述】:

在 Spring 应用程序中,可以使用默认自动装配的模拟 bean 编写测试,并且可以使用通常的 Mockito 方法进一步自定义。为此,使用了 @MockedBean 注释。 然而,当只使用没有 Spring 的 Mockito 时,可以配置模拟以打印所有交互,请参阅How to use Mockito to show all invocations on a mock。 使用模拟 bean 也可以进行这种调试吗?

【问题讨论】:

    标签: spring testing mockito


    【解决方案1】:

    没有 Sprint 原生方式,但可以使用 Mockito 的 mockingDetails(<some mock>) 方法来检索模拟的 MockingDetails,然后打印交互。将调试放在@After 注解的方法中,以便每次都显示,即使测试失败。

    import static org.mockito.Mockito.mockingDetails;
    import org.junit.After;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.test.context.junit4.SpringRunner;
    import com.fasterxml.jackson.core.JsonProcessingException;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(
    public class MyTest {
        @MockBean
        LoginDAO loginDaoMock;
    
        @Test
        public void xyz()  {
        // some test
        }
    
        @After
        public void showInteractions() {
            System.out.println(mockingDetails(loginDaoMock).getInvocations());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2019-01-07
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2015-12-19
      • 2018-06-10
      相关资源
      最近更新 更多