文章摘要: 单元测试。
- 创建springboot项目
- 添加
spring-boot-test和spring-boot-web场景启动器,实现项目单元测试
<dependency> <!-- 单元测试启动器 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency> <!-- Web开发启动器 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 在项目中添加相关注解
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
@SpringBootTest
@AutoConfigureMockMvc
public class MockMvcTests {
@Autowired
MockMvc mockMve;
@Test
public void testMockMVC() {
// 发起一个模拟请求,不依赖网络,不依赖web服务,不需要启动web应用。
mockMvc.perform {
}
}
}