多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 简介 [http://www.ypk1226.com/2018/11/20/spring-boot/spring-boot-test-2/](http://www.ypk1226.com/2018/11/20/spring-boot/spring-boot-test-2/) # 添加依赖 ~~~ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ~~~ # 编写测试类 ~~~ @RunWith(SpringRunner.class) @SpringBootTest class UserRepositoryTest { @Autowired private UserMapper userMapper; @Test public void test() { //.............. } } ~~~ ~~~ @SpringBootTest(classes = StartUpApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) ~~~ 其中,`classes`属性指定启动类,`SpringBootTest.WebEnvironment.RANDOM_PORT`经常和测试类中`@LocalServerPort`一起在注入属性时使用。会随机生成一个端口号。