spring web 测试有三种方式

1. 自己初始化 MockMvc

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

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

spring web 最常用导入静态方法

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

junit 5断言。

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

一、启用测试用例

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

二、初始化mvc

    private MockMvc mockMvc;

    @Autowired
private WebApplicationContext wac; @Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}

三、常用方法

this.mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML)).andDo(print());
@Test
public void testForm() throws Exception{
this.mockMvc.perform(post("/").param("text", "text").param("summary", "summary"))
.andExpect(status().is3xxRedirection()).andDo(print());
}
/**
* 对于 @Valid 无效,后续解决了进行补充
* @throws Exception
*/
@Test
public void testFormFail() throws Exception{
this.mockMvc.perform(post("/").param("text", "text").param("summary",""))
.andDo(print());
}

如果使用  @SpringBootTest 则可以使用

    @Autowired
private WebTestClient webTestClient; @Test
public void test1CreateGithubRepository() {
RepoRequest repoRequest = new RepoRequest("test-webclient-repository", "Repository created for testing WebClient"); webTestClient.post().uri("/api/repos")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.body(Mono.just(repoRequest), RepoRequest.class)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody()
.jsonPath("$.name").isNotEmpty()
.jsonPath("$.name").isEqualTo("test-webclient-repository");
}

使用 webClient

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

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. AcWing 前缀和 一维加二维

    一维 #include<bits/stdc++.h> using namespace std; ; int n,m; int a[N],s[N]; int main(){ ios::syn ...

  2. 以管理员身份运行 Microsoft Edge 时不支持登录

    之前一直用 edge chromium bate版本 一直不能登录 今天试了正式版 还是这样 然后百度一大堆没找到解决方案 设置兼容性为windown7 是可以的 但是 UI显示会有问题 再次打开 又 ...

  3. myeclipse2017配置tomcat7.0

    具体配置参考这篇博客:https://www.cnblogs.com/alibaba-inc/p/9249135.html 期间可能会碰到这样一个问题,"The server does no ...

  4. Game of Credit Cards

    After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle betwe ...

  5. VS“无法查找或打开PDB文件”是怎么回事?如何解决

    有时候,我们使用 VS(Visual Studio)编译程序时会出现“无法查找或打开PDB文件”的提示,并且此时程序会生成失败,无法运行,如下图所示: 大家不要惊慌,出现这种提示并不是代码写错了,而是 ...

  6. UVA 1267 Network(DFS)

    题目链接:https://vjudge.net/problem/UVA-1267 首先我们要把这样一棵无根树转换成有根树,那么树根我们可以直接使用$VOD$. 还有一个性质:如果深度为$d$的一个节点 ...

  7. 【做题笔记】CF1311A、B、C

    或许以后会有D. A 题目大意:给定两个整数 \(a,b\) ,每次可以进行一下任意一个操作: \(a\) 加上任意一个正奇数 \(b\) 减去任意一个正偶数 问是否可以通过若干次操作把 \(a\) ...

  8. Spring 事务归纳

    Spring transaction 什么是事务 A用户向B用户转帐100,第一步要从A帐户扣出100,第二步要将B帐户加上100.其中无论是第一步失败,还是第二步失败.都应该将A.B帐户的余额保持和 ...

  9. python笔记17

    1.今日内容 迭代器(3*) 生成器(4*) 装饰器(5*) 项目结构 logging模块 2.内容回顾 & 作业 2.1 内容回顾 2.1.1 函数(内置/自定义) 基本函数结构 def f ...

  10. ubuuntu截图

    方法1: 按 print screen sysrq 方法2: 系统设置 选择键盘 选择快捷键窗口 选择截图 按照自己的习惯更改快捷键即可.