常用的测试框架
Junit
Junit4
参考资料
- 官网:https://junit.org/junit4/
- 引入:https://blog.csdn.net/gakki_200/article/details/106413351
- 使用说明:https://blog.csdn.net/weixin_39584888/article/details/111938896
@Test注解
import org.junit.Test;
import static org.junit.Assert.*;
public class StudentOperationMySqlImplTest {
IStudentDAO studentDAO = (IStudentDAO) DAOFactory.newInstance("IStudentDAO");
@Test//@Test注解
public void addStudent() {
Student student = new Student(1007l, "赵六", 17, "女", DateUtils.strToUtil("2002-02-13"), "北京市海淀区");
assertEquals(new Integer(1),studentDAO.addStudent(student));//断言测试
}
}
除此之外,还有@Before(每一个测试方法调用前必执行的方法)等注解
详见:https://zhuanlan.zhihu.com/p/86624354
Springboot
在Springboot2.2之前,集成了Junit4(之后是junit5)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Service层
@RunWith(SpringRunner.class)
@SpringBootTest
public class LearnServiceTest {
@Autowired
private LearnService learnService;
@Test
public void getLearn(){
LearnResource learnResource=learnService.selectByKey(1001L);
Assert.assertThat(learnResource.getAuthor(),is("博客"));
}
}
Controller层
MockMvc实现了对Http请求的模拟
@RunWith(SpringRunner.class)
@SpringBootTest
public class LearnControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mvc;
@Before
public void setupMockMvc(){
mvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
}
/**
* 新增教程测试用例
* @throws Exception
*/
@Test
public void addLearn() throws Exception{
String json="{\"author\":\"HAHAHAA\",\"title\":\"Spring\",\"url\":\"http://tengj.top/\"}";
mvc.perform(MockMvcRequestBuilders.post("/learn/add")
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(json.getBytes()) //传json参数
.session(session)
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print());
}
}
Junit4和Junit5的区别
- 忽略测试用例执行
- RunWith 配置
- @Before、@BeforeClass、@After、@AfterClass 被替换
Junit5
参考资料
- 官网:https://junit.org/junit5/
SpringBoot
在Springboot2.2之后,集成了Junit5
参考资料:
- https://blog.csdn.net/weixin_59654772/article/details/123309325
- https://cloud.tencent.com/developer/article/1879367
Controller层
@SpringBootTest
class UploadFileControllerTest {
@Autowired
private UploadFileController fileController;
@Test
void getFileList() throws Exception {
Result result = fileController.getFileList();
System.out.println(result);
}
}
或者
@SpringBootTest
class UploadFileControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@BeforeEach
void init() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).alwaysDo(print()).build();
}
@Test
void getFileList() throws Exception {
mockMvc.perform(get("/fileList")).andExpect(status().isOk());
}
}
或者
@SpringBootTest
@AutoConfigureMockMvc
class UploadFileControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void getFileList() throws Exception {
mockMvc.perform(get("/fileList")).andExpect(status().isOk());
}
}
Service层/dao层
一般是对接口进行测试
@SpringBootTest
class FileMapperTest {
@Autowired
private FileMapper fileMapper;
@Test
void selectFileList() {
List<FilePO> filePOS = fileMapper.selectFileList();
for (FilePO filePO : filePOS) {
System.out.println(filePO);
}
}
}