SpringBoot Controller Post接口单元测试示例
2018-12-09 20:04
1381 查看
概述
在日常的开发中,我们一般会定义一个service
层,用于实现业务逻辑,并且针对service
层会有与之对应的齐全的覆盖率高的单元测试。而对于controller
层,一般不怎么做单元测试,因为主要的核心业务逻辑都在service
层里,controller
层只是做转发,调用service
层接口而已。但是还是建议使用单元测试简单的将controller
的方法跑一下,看看转发和数据转换的代码是否能正常工作。
在Spring Boot
里对controller
层进行单元测试非常简单,只需要几个注解和一点点辅助代码即可搞定。
依赖的包
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency>
使用的Spring Boot 版本
2.0.4.RELEASE
代码
@ExtendWith(SpringExtension.class)@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class) @AutoConfigureMockMvc public class UserControllerTest { @Autowired private MockMvc mockMvc;@MockBean private UserService userService; @Test @DisplayName("测试controller方法") void test() throws Exception { User param = new User(); param.setUserId(1111); List<Address> addressList = new ArrayList<>(); Address address = new Address(); address.setName("我的地址"); addressList.add(address); param.setAddressList(addressList); MvcResult mvcResult = mockMvc.perform( post("/xxx/test") .contentType(MediaType.APPLICATION_JSON) .content(JSON.toJSONString(param))) .andReturn(); System.out.println(mvcResult.getResponse().getContentAsString()); } }
@RequestMapping(value = "/xxx", method = RequestMethod.POST) public Object test(@RequestBody(required = false)User user) throws Exception { }
如果你只是想简单的跑一下controller
层,不想真正的去执行service
方法的话,需要使用@MockBean
将对应的service
类mock
掉。
@MockBean private UserService userService;
使用Spring Boot Test
的时候,它需要一个ApplicationContext
,我们可以在@SpringBootTest
注解中使用classes
属性来指定。
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class)
TestApplication
的代码很简单。
@SpringBootApplication public class TestApplication { public static void main(String[] args){ SpringApplicationBuilder builder = new SpringApplicationBuilder(); builder.environment(new StandardEnvironment()); builder.sources(TestApplication.class); builder.main(TestApplication.class); builder.run(args); } }
接下来我们只需要使用MockMvc
发送post
请求即可。如果controller
层的post
方法是带@RequestBody
注解的,可以先将入参对象转换成JSON
字符串。这里使用的是fastjson
。
JSON.toJSONString(param)
经过测试,如上代码能正常工作。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:
- 详解springboot整合Listener的两种方式
- Springboot整合Shiro之加盐MD5加密的方法
- springboot2.0集成rabbitmq的示例代码
- 如何解决SpringBoot2.x版本对Velocity模板不支持的方案
- Centos下SpringBoot项目启动与停止脚本的方法
- 详解springboot之jackson的两种配置方式
- SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
- springboot整合mybatis中的问题及出现的一些问题小结
- 详解springboot设置cors跨域请求的两种方式
- 详解在springboot中使用Mybatis Generator的两种方式
- Spring Boot单元测试中使用mockito框架mock掉整个RedisTemplate的示例
相关文章推荐
- SpringBoot HTTP接口 POST、PUT、DELETE 请求
- springboot+thymeleaf国际化之LocaleResolver接口的示例
- Springmvc Controller接口代码示例
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子
- 基于Springboot+Junit+Mockito做单元测试的示例
- SpringBoot接口使用MockMvc进行单元测试
- Spring-boot 配置Aop获取controller里的request中的参数以及其返回值
- SpringBoot学习之整合SSM+pagehelper+druid+单元测试
- spring boot 单元测试
- spring boot 中访问 REST 接口
- Spring Boot使用模板freemarker的示例代码
- SpringBoot实践之---单元测试
- Whitelabel Error Page(按spring-boot官网的示例跑不起来的原因之一)
- Spring Boot 的单元测试
- Spring Boot(三)之找回熟悉的Controller,Service
- 基于Spring Boot,Security和JWB的REST接口的无状态认证
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
- Spring boot CommandLineRunner接口使用例子
- Spring Boot+AngularJS+BootStrap实现进度条示例代码