# Spring boot 使用单元测试
# 1.pom.xml新增依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.2.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 2.新建测试方法
包名需和实际方法的包名保持一致
@RunWith(value = SpringRunner.class)
@SpringBootTest(classes = App.class)//App.class为实际代码启动类,
//@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//如果项目使用了websocket,需要加上webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,否则会报错
public class AppTest {
@Test
public void test() {
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10