如何使用 Mockito 模拟并使用 Junit5 断言抛出的异常和消息?
How to mock using Mockito and assert a thrown exception and message using Junit5?
我正在使用 Mockito 以及 Junit5 和 SpringBootTest。我需要断言抛出的异常及其消息。
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。
我正在使用 Mockito 以及 Junit5 和 SpringBootTest。我需要断言抛出的异常及其消息。
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。