这篇文章主要介绍了SpringBoot MockMvc单元测试的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
为何使用MockMvc?
对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便,依赖网络环境等,所以为了可以对Controller进行测试,我们引入了MockMVC。
MockMvc实现了对Http请求的模拟,能够直接使用网络的形式,转换到Controller的调用,这样可以使得测试速度快、不依赖网络环境,而且提供了一套验证的工具,这样可以使得请求的验证统一而且很方便。
MockMvcBuilder
MockMvc是spring测试下的一个非常好用的类,他们的初始化需要在setUp中进行。
MockMvcBuilder是用来构造MockMvc的构造器,其主要有两个实现:StandaloneMockMvcBuilder和DefaultMockMvcBuilder,前者继承了后者。
① MockMvcBuilders.webAppContextSetup(WebApplicationContext context):指定WebApplicationContext,将会从该上下文获取相应的控制器并得到相应的MockMvc;
② MockMvcBuilders.standaloneSetup(Object… controllers):通过参数指定一组控制器,这样就不需要从上下文获取了,比如this.mockMvc = MockMvcBuilders.standaloneSetup(this.controller).build();
这些Builder还提供了其他api,可以自行百度
1.添加测试依赖
4.0.0top.ythengspringboot-demo0.0.1-SNAPSHOTjar org.springframework.bootspring-boot-starter-parent2.0.5.RELEASE<!-- lookup parent from repository --> UTF-8UTF-81.8 org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-starter-testtest org.springframework.bootspring-boot-starter-thymeleaf org.springframework.bootspring-boot-devtoolstruetrue org.springframework.bootspring-boot-maven-plugin
2.添加MocMvc测试类
package top.ytheng.demo; import org.junit.Test; import org.junit.runner.RunWith; 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.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //底层用junit SpringJunit4ClassRunner @RunWith(SpringRunner.class) //启动整个Springboot工程 @SpringBootTest(classes= {DemoApplication.class}) //鼠标选中SpringBootTestDemo后执行Run As -> JUnit Test可以同时执行多个测试 @AutoConfigureMockMvc public class MockMvcTestDemo { @Autowired private MockMvc mockMvc; @Test public void mockTest() throws Exception { MvcResult mockResult = mockMvc.perform(MockMvcRequestBuilders.get("/file/testpath")). andExpect(MockMvcResultMatchers.status().isOk()).andReturn(); int status = mockResult.getResponse().getStatus(); System.out.println(status); } }
3.添加测试控制器
package top.ytheng.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/file") public class FileController { @RequestMapping("/testpath") @ResponseBody private Object testPath() { return file<i style="color:transparent">来源gaodai$ma#com搞$$代**码)网</i>Path; } }
4.添加启动类
package top.ytheng.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //等于下面3个 //@SpringBootConfiguration //@EnableAutoConfiguration //@ComponentScan public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
5.鼠标选中MockMvcTestDemo类名,使用快捷键启动JUnit测试(Shift + Alt + X,T),进行测试
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网。
以上就是SpringBoot MockMvc单元测试的示例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!