参考
- https://blog.csdn.net/wangxi06/article/details/114630426
- https://blog.csdn.net/qq_44381387/article/details/120869168 (新版spring-boot-starter-test不再集成junit,而是junit-jupiter,无需 @RunWith)
- https://www.jianshu.com/p/34f57f41af70
- https://www.cnblogs.com/qdhxhz/p/13684458.html
- https://blog.csdn.net/pan_junbiao/article/details/104296855
- https://blog.csdn.net/ryo1060732496/article/details/80792246
- https://junit.org/junit5/ (junit5官网)
- https://blog.csdn.net/qq_38928944/article/details/82220016 (assertThat用法)
- https://blog.csdn.net/wangxi06/article/details/114630426
- https://zhuanlan.zhihu.com/p/428798586
- https://blog.csdn.net/fanxiaobin577328725/article/details/78407192
- https://juejin.cn/post/7023706905570705415
注意
- 新版spring-boot-starter-test不再集成junit,而是junit-jupiter,无需 @RunWith(本文章使用的版本 2.3.12.RELEASE)
- vs code 插件由于安装了无法提供更准确的插件名称,应该罗列的这几个就是。
环境
环境 | 版本 | 操作 |
---|---|---|
windows | 10 | |
vs code | 1.84.2 | |
Spring Boot Extension Pack | v0.2.1 | vscode插件 |
Extension Pack for Java | v0.25.15 | vscode插件 |
JDK | 11 | |
Springboot | 2.3.12.RELEASE |
正文
junit5
仅仅一部分示例,具体请参考官网
package com.example.demo.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import javax.annotation.Resource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.entity.UserEntity;
@SpringBootTest
public class UserServiceTest {
@Resource
UserService userService;
@BeforeAll
static void beforeAll(){
System.out.println("==beforeAll==");
}
@AfterAll
static void afterAll(){
System.out.println("==AfterAll==");
}
@BeforeEach
void beforeEach(){
System.out.println("==BeforeEach==");
}
@AfterEach
void afterEach(){
System.out.println("==AfterEach==");
}
@Test
void testAdd() {
UserEntity userEntity = userService.add();
// 相等
assertEquals(userEntity.getId(), 1);
// 不相等
assertNotEquals(2, 1);
// 对象是为空
assertNull(null);
// 断言为真
assertTrue(true);
}
}
控制器测试 WebMvcTest
仅仅一部分简单示例。
- src\main\java\com\example\demo\controller\IndexController.java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
@RequestMapping("/")
public class IndexController {
@GetMapping("test")
public String test() {
return "hello";
}
}
- src\test\java\com\example\demo\controller\IndexControllerTest.java
package com.example.demo.controller;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.assertj.core.data.Index;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
// @SpringBootTest
// 告诉 JUnit 5 启用 Spring 支持
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = IndexController.class)
public class IndexControllerTest {
/**
* 加上 @WebMvcTest(controllers = IndexController.class) 就可以自动注入,不加就要在 @BeforeEach 手动注入。
*/
@Autowired
private MockMvc mvc;
// @BeforeEach
// public void setUp() throws Exception {
// mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
// System.out.println("初始化mock模块");
// }
@Test
void testTest() throws Exception {
String responseString = mvc.perform(
MockMvcRequestBuilders.get("/test")
.accept(MediaType.APPLICATION_JSON)
)
.andExpect(MockMvcResultMatchers.status().isOk()) //返回的状态是200
.andReturn().getResponse().getContentAsString(); //将相应的数据转换为字符串;
System.out.println("获取结果为:" + responseString);
assertEquals(responseString, "hello");
}
}
标签:单元测试,springframework,jupiter,Springboot2,import,test,org,转载,junit
From: https://www.cnblogs.com/xiaqiuchu/p/17900751.html