spring-boot web测试层学习记录
来源:互联网 发布:最全的淘宝隐藏券网站 编辑:程序博客网 时间:2024/06/06 12:25
spring boot web层测试
Author : Janloong Do_O
依赖注入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency>
测试层
controller基础测试
单次测试,实例化context一次 ,多次测试间不共用context,每次生成一个
若需要多次测试间共用一个context , 需要在类级上使用注解 @DirtiesContext
该部分context理解还不够,可能不正确
class
@RunWith(SpringRunner.class)@SpringBootTest
params
@Autowiredprivate A a;
method
@TestassertThat(controller).isNotNull();
模拟http请求测试
class
@RunWith(SpringRunner.class)<!-- 自定义端口 -->@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
params
@LocalServerPortprivate int port;@Autowiredprivate TestRestTemplate restTemplate;
method
@Test assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/", String.class)).contains("Hello World");
仿真多http应用级请求测试
class
@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureMockMvc这里针对实际web层请求可以使用注解@RunWith(SpringRunner.class)@WebMvcTest单个注入时@WebMvcTest(a-controller.class)
params
@Autowired private MockMvc mockMvc;
method
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()) .andExpect(content().string(containsString("Hello World")));
依赖性controller层注入
class
@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureMockMvc
params
@Autowiredprivate MockMvc mockMvc;@MockBeanprivate GreetingService greetingService;
method
when(greetingService.greet()).thenReturn("Hello Mock");this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk()) .andExpect(content().string(containsString("Hello Mock")));
阅读全文
0 0
- spring-boot web测试层学习记录
- spring 测试层学习记录
- spring boot 学习--03---web控制层全局异常处理
- Spring boot学习记录
- spring boot 学习(三)API注解记录及测试
- spring boot 学习(三)API注解记录及测试
- Spring Boot Web API测试
- spring boot写单元测试(测试service层)
- SPRING-BOOT入门学习记录
- Spring Boot学习记录(四)--问题记录
- Spring Boot学习记录(四)--问题记录
- Spring-Boot学习(2)-----Spring-Boot-web学习
- Spring Boot学习记录(一)--环境搭建
- Spring Boot学习记录(二)--thymeleaf模板
- Spring Boot学习记录(三)--整合Mybatis
- Spring Boot学习记录–thymeleaf模板
- Spring Boot学习记录(二)--thymeleaf模板
- Spring Boot学习记录(二)--thymeleaf模板
- ARM GCC 内嵌(inline)汇编手册
- Python的闭包(Closure)与惰性计算(Lazy Evaluation)
- 启动欢迎页面和广告图片轮转
- jfinal集成swagger
- io流处理的核心类的详解
- spring-boot web测试层学习记录
- 对单链表的排序(选择排序)
- Unity中Lerp与SmoothDamp函数使用误区浅析
- 脱氧核糖核酸(DNA)由两条互补的碱基链以双螺旋的方式结合而成
- CTF常见题型
- Eclipse无法查看jdk源码解决方法
- ionic项目实战-小白踩坑走起
- 创建动态链表的三种算法(C语言实现)——正向、逆向、递归
- 【Swift4.0】集合类型-Array