1、pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
2、在test目录下建立单元测试class文件:包层级最好与业务代码一致
3、单元测试代码demo:
@SpringBootTest(classes = {项目启动类.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class MyJobTest {
@Autowired
private UserService userService;
@Test
public void handleData(){
try {
userService.handleData();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、运行:点击对应方法边上的启动按钮即可运行:
不需要提前启动项目。