导入依赖启动器
springboot初始化器默认导入junit5
在这里插入图片描述
常用测试注解
@SpringBootTest//如果标注这个 测试无法使用springboot的自动注入
/**
* springboot junit的单元的测试
*/
@DisplayName("junit5测试类")
class JiaqiselflearnApplicationTests {
@Autowired
private deptMapper deptmapper;
@Test
void contextLoads() {
System.out.println(deptmapper);
}
@Test
public void test(){
int conuntall = deptmapper.conuntall();
System.out.println(conuntall);
}
@Transactional//标注测试方法会自动回滚数据
@Test//junt5的Test注解
public void test2(){
System.out.println("测试start");
}
@DisplayName("测试display注解")//测试方法命名
@Test
public void testjunit5(){
System.out.println(1);
}
@BeforeEach//每个测试方法开始前使用
public void testbeaforeeach(){
System.out.println("测试就要开始了");
}
@AfterEach//每个测试方法测试之后执行
public void after(){
System.out.println("任一一个测试方法执行后开始执行");
}
@BeforeAll//所有方法执行前 也就是说只执行一次故此设计为static
static void beforeall(){
System.out.println("所有测试都快开始了");
}
@AfterAll//所有方法执行前 也就是说只执行一次故此设计为static
static void AFTERall(){
System.out.println("所有测试都结束");
}
@Timeout(value = 50,unit = TimeUnit.SECONDS)//超时值 单位
@Test
@DisplayName("启动所有测试")
public void teststartalltest(){
test();
test2();
testjunit5();
contextLoads();
}
/**
*重复测试注解
*/
@RepeatedTest(5)
public void testrepeat(){
System.out.println("测试重复注解 输出5次");
}
}
重复测试运行结果