博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring、Spring Boot和TestNG测试指南 - 测试Spring MVC
阅读量:7086 次
发布时间:2019-06-28

本文共 3017 字,大约阅读时间需要 10 分钟。

提供了,能够很方便的来测试Controller。同时Spring Boot也提供了更进一步简化了测试需要的配置工作。

本章节将分别举例说明在不使用Spring Boot和使用Spring Boot下如何对Spring MVC进行测试。

例子1:Spring

测试Spring MVC的关键是使用MockMvc对象,利用它我们能够在不需启动Servlet容器的情况下测试Controller的行为。

源代码SpringMvc_1_Test.java:

@EnableWebMvc@WebAppConfiguration@ContextConfiguration(classes = { FooController.class, FooImpl.class })public class SpringMvc_1_Test extends AbstractTestNGSpringContextTests {  @Autowired  private WebApplicationContext wac;  private MockMvc mvc;  @BeforeMethod  public void prepareMockMvc() {    this.mvc = webAppContextSetup(wac).build();  }  @Test  public void testController() throws Exception {    this.mvc.perform(get("/foo/check-code-dup").param("code", "123"))        .andDo(print())        .andExpect(status().isOk())        .andExpect(content().string("true"));  }}

在这段代码里,主要有三个步骤:

  1. 将测试类标记为@WebAppConfiguration

  2. 通过webAppContextSetup(wac).build()构建MockMvc

  3. 利用MockMvc对结果进行判断

例子2:Spring + Mock

在例子1里,FooController使用了一个实体FooImpl的Bean,实际上我们也可以提供一个Foo的mock bean来做测试,这样就能够更多的控制测试过程。如果你还不知道Mock那么请看Chapter 3: 使用Mockito。

源代码SpringMvc_2_Test.java:

@EnableWebMvc@WebAppConfiguration@ContextConfiguration(classes = { FooController.class })@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)public class SpringMvc_2_Test extends AbstractTestNGSpringContextTests {  @Autowired  private WebApplicationContext wac;  @MockBean  private Foo foo;  private MockMvc mvc;  @BeforeMethod  public void prepareMockMvc() {    this.mvc = webAppContextSetup(wac).build();  }  @Test  public void testController() throws Exception {    when(foo.checkCodeDuplicate(anyString())).thenReturn(true);    this.mvc.perform(get("/foo/check-code-dup").param("code", "123"))        .andDo(print())        .andExpect(status().isOk())        .andExpect(content().string("true"));  }}

例子3:Spring Boot

Spring Boot提供了@WebMvcTest更进一步简化了对于Spring MVC的测试,我们提供了对应例子1的Spring Boot版本。

源代码BootMvc_1_Test.java:

@WebMvcTest@ContextConfiguration(classes = { FooController.class, FooImpl.class })public class BootMvc_1_Test extends AbstractTestNGSpringContextTests {  @Autowired  private MockMvc mvc;  @Test  public void testController() throws Exception {    this.mvc.perform(get("/foo/check-code-dup").param("code", "123"))        .andDo(print())        .andExpect(status().isOk())        .andExpect(content().string("true"));  }}

在这里,我们不需要自己构建MockMvc,直接使用@Autowired注入就行了,是不是很方便?

例子4:Spring Boot + Mock

这个是对应例子2的Spring Boot版本,源代码BootMvc_2_Test.java:

@WebMvcTest@ContextConfiguration(classes = { FooController.class })@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)public class BootMvc_2_Test extends AbstractTestNGSpringContextTests {  @Autowired  private MockMvc mvc;  @MockBean  private Foo foo;  @Test  public void testController() throws Exception {    when(foo.checkCodeDuplicate(anyString())).thenReturn(true);    this.mvc.perform(get("/foo/check-code-dup").param("code", "123"))        .andDo(print())        .andExpect(status().isOk())        .andExpect(content().string("true"));  }}

参考文档

转载地址:http://cuwql.baihongyu.com/

你可能感兴趣的文章
为SeekBar滑块设置固定值以及自定义Seekbar,progressbar样式
查看>>
其他软件技巧收藏
查看>>
打开android虚拟机时出现a repairable android virtual device
查看>>
web性能测试的新利器 - Gatling 介绍
查看>>
今日头条屏幕适配方案终极版正式发布!
查看>>
国家粮食和物资储备局部署东北秋粮收购 避免卖粮难
查看>>
有节操的设计多参数方法
查看>>
上海科学家揭示“发烧提高免疫”新机制
查看>>
拒绝“泰囧”!国足一场胜利,竟让我们苦等15年……
查看>>
浙江嵊州根雕传承路:政府艺人联心 演绎“小城大艺”
查看>>
上海医护携手演员缓解儿童就医焦虑 互动体验剧首演
查看>>
突发!新华视点发声,虚拟货币反攻势头戛然而止
查看>>
Android Emulator 推出 Quick Boot 功能
查看>>
design pattens - Bridge
查看>>
腾讯荣获OSCAR尖峰开源企业奖 TARS项目与信通院正式启动合作
查看>>
使用开源技术构建有赞分布式 KV 存储服务
查看>>
女工程师独家揭秘:支撑双11每秒10万次交易背后的数据库团队故事
查看>>
[Day 2]上海CNUTCon全球运维技术大会2017实录
查看>>
搞事情之初识 Docker 与尝试构建 Swift
查看>>
跟我一起部署和定制 CNPM——自定义包存储层
查看>>