🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[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`一起在注入属性时使用。会随机生成一个端口号。