【问题标题】:How to mock using Mockito and assert a thrown exception and message using Junit5?如何使用 Mockito 模拟并使用 Junit5 断言抛出的异常和消息?
【发布时间】:2020-11-20 01:53:43
【问题描述】:

我将 Mockito 与 Junit5 以及 SpringBootTest 一起使用。我需要断言抛出的异常及其消息。

【问题讨论】:

    标签: java mockito junit5 spring-boot-test


    【解决方案1】:
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.function.Executable;
    
    
    @SpringBootTest
    class Sample{
    
       @Autowired
       TestService service;
    
       @Test
       void sampleTest(){
          Executable exec = ()-> service.execute();
          Throwable thrown = assertThrows(MyException.class, exec, "Type your message");        
          assertEquals("Dancing stars", thrown.getMessage());
        }
    }
    

    请参考文档here

    【讨论】:

      猜你喜欢
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2019-07-25
      • 2022-01-23
      • 1970-01-01
      • 2018-09-26
      相关资源
      最近更新 更多