单元测试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
@SpringBootTest
class Boot05WebAdminApplicationTests {
@Test
void contextLoads() {
Long count = jdbcTemplate.queryForObject("select count(*) from account_tb1",Long.class);
log.info("记录数是:{}",count);
log.info("数据类:{}",dataSource.getClass());
}
}
SpringBoot整合Junit以后
- 编写测试方法:@Test标注(注意需要使用junit版本的注解)
- Junit类具有Spring的功能,@Autowired、比如@Transactional 标注测试方法,测试完自动回滚
2.Junit5常用注解
- @DisplayName:为测试类或者测试方法设置展示名称
- @BeforeEach:表示在每个单元测试之前执行
- @AfterEach:表示在每个单元测试之后执行
- @BeforAll:表示在所有单元测试之前执行,方法类型要使用static
- @AfterAll:表示所有单元测试之后执行,方法类型要使用static
- @Tag:表示单元测试类别
- @Disabled:表示单元测试类或测试方法不执行
- @Timeout:表示测试方法运行如果超时就会返回错误
- @ExtendWith:为测试类或测试方法提供扩展类引用