准备
1.control
@RestController
@RequestMapping("/demo")
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping("/hello")
public String hello() {
return "suc!!";
}
@RequestMapping("/person")
public Person getPerson() {
return personService.getPerson();
}
}
2.service
public interface PersonService {
Person getPerson();
}
@Service
public class PersonServiceImpl implements PersonService {
@Override
public Person getPerson() {
return Person.builder().pId(1).pName("测试人员").build();
}
}
3.service
@Data
@Builder
public class Person {
private Integer pId;
private String pName;
}
1.Junit5简介
JUnit 5跟以前的JUnit版本不一样,它由几大不同的模块组成,这些模块分别来自三个不同的子项目。
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
- JUnit平台,其主要作用是在JVM上启动测试框架。它定义了一个抽象的TestEngineAPI来定义运行在平台上的测试框架,同时还支持通过命令行、Gradle和Maven来运行平台。
- JUnit Jupiter,包含了JUnit5最新的编程模型和扩展机制。
- JUnit Vintage,允许在平台上运行JUnit3和JUnit4的测试用例。
- JUnit5对Java运行环境的最低要求是Java8,同时也兼容测试旧版本JDK编译出来的代码。
1.1 添加依赖
切记,springboot2.2之前的版本,若只是添加@SpringBootTest注解,注入是不会生效的,也就是说2.2以后,才支持junit5
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
1.2 使用
@SpringBootTest
@Slf4j
public class ServiceTest {
@Resource
PersonService personService;
@Test
public void testService(){
log.info(personService.getPerson().toString());
}
}
2.Junit4简介
2.1 引入依赖
这里还是使用2.1版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.15.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--排除junit引擎,后续可以使用spring和junit4整合的那一套-->
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2.2 service层访问
//Junit5这里只写SpringBootTest这个注解; 如果是junit4的话, 就要加上@RunWith(SpringRunner.class),指定了junit和spring的整合类
@RunWith(SpringJUnit4ClassRunner.class)
//需要指明当前启动类,加载上下文环境
@SpringBootTest(classes = AppMain.class)
@Slf4j
public class ServiceTest {
@Resource
PersonService personService;
@Test
public void testService(){
log.info(personService.getPerson().toString());
}
}
2.3 control层访问
@RunWith(SpringJUnit4ClassRunner.class)
@AutoConfigureMockMvc //相当于new MockMvc,提供自动注入
@SpringBootTest(classes = AppMain.class)
@Slf4j
public class ServiceTest {
//mock对象
@Resource
private MockMvc mockMvc;
@Resource
PersonService personService;
@SneakyThrows
@Test
public void testControl(){
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/demo/hello"))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
String contentAsString = mvcResult.getResponse().getContentAsString();
log.info(contentAsString);
}
}
参考
SPRING BOOT JUNIT5单元测试@EXTENDWITH无法工作
Spring Boot+JUnit5+Mockito单元测试
springboot2 junit4测试controller
spring boot 单元测试使用new MockMvc和@RunWith+@AutoConfigureMockMvc注解的区别