SpringBoot2.2之后用的Junit5,所以在这里使用的Junit5。Spring Boot会默认帮我们导入包,所以不用添加依赖了。
注解:
@BeforeAll : 只执行一次,执行时机是在所有测试和 @BeforeEach 注解方法之前。
@BeforeEach:在每个测试执行之前执行。
@AfterEach :在每个测试执行之后执行。
@AfterAll:只执行一次,执行时机是在所有测试和 @AfterEach 注解方法之后。
测试一下,代码如下
packagecom.example.demo;import org.junit.jupiter.api.*;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.web.servlet.MockMvc;
@SpringBootTestclassDemoApplicationTests {privateMockMvc mockMvc;
@BeforeAllpublic static voidbeforeAll(){
System.out.println("begin All");
}
@BeforeEachpublic voidbeforeEach(){
System.out.println("begin Each");
}
@Testpublic voidtest1(){
System.out.println("test1");
}
@