[英]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());
}
}
請在此處參考文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.