SpringBoot-5整合JUnit

SpringBoot-5整合JUnit

JUnit是执行单元测试使用的,导入坐标,写测试类,并在测试类加一些注解

1. JUnit坐标导入

导入JUnit的坐标(可能如果是高版本的SpringBoot就把这个集成了,这样会Junit4)

1
2
3
4
5
6
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

2. Test类里面测Service Controller

首先在这里创建一个test类,和Service名称前缀一样src/test/java/cn/edu/bupt/aiswitchboard/NameFinderServiceTest.java

在里面可以测Service,也可以测Controller,自动装配相对应的

除了预期的输入输出,可能非预期的输入输出也要给写上,包括报出来的错误类型是什么

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cn.edu.bupt.aiswitchboard;

import cn.edu.bupt.aiswitchboard.common.Response;
import cn.edu.bupt.aiswitchboard.controller.NameFinderController;
import cn.edu.bupt.aiswitchboard.service.NameFinderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;

/**
* NameFinder的测试类,可以测Service也可以测Controller
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class NameFinderServiceTest {
@Autowired
private NameFinderService nameFinderService;

@Autowired
private NameFinderController nameFinderController;

@Test
public void testNameFinderService() {
// 除了预期的输入输出,非预期的也要写上,报什么错返回什么,如果能传的话
Integer a = nameFinderService.test(5);
assertEquals((Integer) 5, a);

a = nameFinderService.test(4);
assertEquals((Integer) 4, a);
}

@Test
public void testNameFinderController() {
Response<Object> resp = nameFinderController.postJsonInput();
Response<Object> tmp = new Response<>();
tmp.update(200, "SUCCESS", null);
assertEquals(tmp, resp);
}
}

SpringBoot-5整合JUnit
http://example.com/2023/04/10/develop/java/springboot/SpringBoot-5整合JUnit/
作者
Curious;
发布于
2023年4月10日
许可协议