整合JUnit
- 导入测试对应的starter
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 测试类使用@SpringBootTest修饰
- 使用自动装配的形式添加要测试的对象
- 测试类如果存在于引导类所在包或子包中无需指定引导类
@SpringBootTest
class Springboot04JunitApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
bookDao.save();
System.out.println("two...");
}
}
- 测试类如果不存在于引导类所在的包或子包中需要通过classes属性指定引导类
@SpringBootTest(classes = Springboot04JunitApplication