在Springboot项目中集成单元测试及其的简单,首先先在pom文件中添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
然后建立单元测试类,如下示例:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserTest {
@Autowired
private UserService userService;
@Test
public void myTest() {
userService.get();
}
}
其中Application.class需要替换成自己的项目的启动类。然后run当前的类即可。