spring web 测试有三种方式

1. 自己初始化 MockMvc

2.依赖@springbootTest 它会帮你生产 webTestClient ,只需自己注入即可。

3.启动的时候,不加载整个应用,进行远程调用   使用WebClient

spring web 最常用导入静态方法

  1. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  2. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  3. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;

junit 5断言。

  1. import static org.junit.jupiter.api.Assertions.*;

一、启用测试用例

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(classes = { WebMvcConfiguration.class, DataConfiguration.class })
  3. @WebAppConfiguration

二、初始化mvc

  1. private MockMvc mockMvc;
  2.  
  3. @Autowired
  4. private WebApplicationContext wac;
  5.  
  6. @Before
  7. public void setup() {
  8. this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  9. }

三、常用方法

  1. this.mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk())
  2. .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML)).andDo(print());
  1. @Test
  2. public void testForm() throws Exception{
  3. this.mockMvc.perform(post("/").param("text", "text").param("summary", "summary"))
  4. .andExpect(status().is3xxRedirection()).andDo(print());
  5. }
  6. /**
  7. * 对于 @Valid 无效,后续解决了进行补充
  8. * @throws Exception
  9. */
  10. @Test
  11. public void testFormFail() throws Exception{
  12. this.mockMvc.perform(post("/").param("text", "text").param("summary",""))
  13. .andDo(print());
  14. }

如果使用  @SpringBootTest 则可以使用

  1. @Autowired
  2. private WebTestClient webTestClient;
  3.  
  4. @Test
  5. public void test1CreateGithubRepository() {
  6. RepoRequest repoRequest = new RepoRequest("test-webclient-repository", "Repository created for testing WebClient");
  7.  
  8. webTestClient.post().uri("/api/repos")
  9. .contentType(MediaType.APPLICATION_JSON_UTF8)
  10. .accept(MediaType.APPLICATION_JSON_UTF8)
  11. .body(Mono.just(repoRequest), RepoRequest.class)
  12. .exchange()
  13. .expectStatus().isOk()
  14. .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
  15. .expectBody()
  16. .jsonPath("$.name").isNotEmpty()
  17. .jsonPath("$.name").isEqualTo("test-webclient-repository");
  18. }

使用 webClient

  1. WebClient webClient = WebClient.builder()
  2. .baseUrl("https://api.github.com")
  3. .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.github.v3+json")
  4. .defaultHeader(HttpHeaders.USER_AGENT, "Spring 5 WebClient")
  5. .build();
  6.  
  7. public Flux<GithubRepo> listGithubRepositories(String username, String token) {
  8. return webClient.get()
  9. .uri("/user/repos")
  10. .header("Authorization", "Basic " + Base64Utils
  11. .encodeToString((username + ":" + token).getBytes(UTF_8)))
  12. .exchange()
  13. .flatMapMany(clientResponse -> clientResponse.bodyToFlux(GithubRepo.class));
  14. }

spring web 测试用例的更多相关文章

  1. 《SSM框架搭建》三.整合spring web

    感谢学习http://blog.csdn.net/zhshulin/article/details/37956105#,还是修改了spring到最新的版本和接口开发示例 根据前一篇日志,已经有了myb ...

  2. Spring Web应用的最大瑕疵

    众所周知, 现在的Spring框架已经成为构建企业级Java应用事实上的标准了,众多的企业项目都构建在Spring项目及其子项目之上,特别是Java Web项目,很多都使用了Spring并且遵循着We ...

  3. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块

    spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...

  4. spring web.xml 难点配置总结

    web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...

  5. Spring web应用最大的败笔

    第一篇 介绍下IOC DI Spring主要是业务层框架,现在已经发展成为一个完整JavaEE开发框架,它的主要特点是IoC DI和AOP等概念的融合,强项在面向切面AOP.推出之初因为Ioc/AOP ...

  6. 菜鸟学习Spring Web MVC之二

    有文章从结构上详细讲解了Spring Web MVC,我个菜鸟就不引据来讲了.说说强悍的XP环境如何配置运行环境~~ 最后我配好的环境Tomcat.Spring Tool Suites.Maven目前 ...

  7. 4.Spring Web MVC处理请求的流程

  8. 1.Spring Web MVC有什么

    Spring Web MVC使用了MVC架构模式的思想,将web层进行职责解耦. 同样也是基于请求驱动的,也就是使用请求-响应模型.它主要包含如下组件: DispatcherServlet :前端控制 ...

  9. Spring Web Flow使用

    就当我写(嘘,抄)着玩的. 使用Spring框架的一个子项目--Spring Web Flow来建立和管理Web应用和UI流程. 第一节:使用Spring Web Flow在一个Spring MVC应 ...

随机推荐

  1. 重新装Mysql后原来数据的恢复办法:即一个版本的mysql的data文件夹下的所有的数据,怎么去加载到另一个安装的mysql中

    重新装Mysql后原来数据的恢复办法本博客:http://blog.csdn.net/xiaowu_zhu/article/details/71188955 不管是重新装系统或者数据库时,总会遇到怎么 ...

  2. ios 下 select和option 无法隐藏指定元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 使用@RunWith(SpringJUnit4ClassRunner.class)进行单元测试时 报错 和 java.lang.NoSuchMethodError的解决方法

    1 使用@RunWith(SpringJUnit4ClassRunner.class)进行单元测试时,需要junit高版本和spring-test的高版本才支持,junit需要4.0以上的,sprin ...

  4. 题解 P6013 【压岁钱】

    月赛\(\text{Div2T1}\),窝唯一一道\(\text{AC}\)的题(我太菜啦!) \(\text{solution:}\) 根据题面,显然三个操作对应三种情况,我们发现每次这三种操作均不 ...

  5. Java学习笔记(八)面向对象---抽象类

    当多个类中出现相同功能,但是功能主体不同时,我们可以向上抽取,只抽取功能定义,而不抽取功能主体. 抽象类的特点 抽象方法一定在抽象类中. 抽象方法和抽象类都必须被abstract关键字修饰. 抽象类不 ...

  6. Java - 字符串操作

    字符串常用操作如下 public static void main(String[] args) { /** * 创建字符串 */ String s1="zifuchuan123" ...

  7. opencv:USM锐化

    USM:unsharp mask 对小的细节干扰小,对大的细节进行锐化 Mat dst; Mat blur_image; GaussianBlur(src, blur_image, Size(3, 3 ...

  8. 简单实现一个button控制两种状态

    <button class="btn a-bgcolor" data-toggle="tooltip" data-placement="left ...

  9. 使用jenkins 实现 .net core项目自动发布到 docker

    在Docker内运行Jenkins pull镜像  docker pull jenkins/jenkins:lts Dockerfile FROM jenkins/jenkins:lts USER r ...

  10. strtok() and strtod()

    1.strtok(参数1,参数2)按指定的分割符将字符串分割开来 参数1:表示要被分割的字符串的地址: 参数2:表示指定的分割符的地址: 例如:按空格分割“Hello World” buffer[] ...