单元测试在正规项目开发过程中是不可或缺的,像sonar之类的工具可以对项目代码的测试覆盖率都可以统计出来,从测试代码覆盖率上就可以从侧面反应出代码整体运行可能出问题的概率(不是一定),所以大型公司项目对单元测试覆盖率都有明确的要求。

作为现在微服务开发基础的springboot,有必要针对这个框架的单元测试进行必要的探讨,尤其是controller接口的单元测试。springboot针对单元测试提供了很多辅助注解,了解了这些注解就可以轻松的使用这些特性进行微服务的单元测试。

本文仅针对controller接口侧面的单元测试进行阐述,首先看下@WebMvcTest这个注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(WebMvcTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
@OverrideAutoConfiguration(enabled = false)
@TypeExcludeFilters(WebMvcTypeExcludeFilter.class)
@AutoConfigureCache
@AutoConfigureWebMvc
@AutoConfigureMockMvc
@ImportAutoConfiguration
public @interface WebMvcTest {/*** Properties in form {@literal key=value} that should be added to the Spring* {@link Environment} before the test runs.* @return the properties to add* @since 2.1.0*/String[] properties() default {};/*** Specifies the controllers to test. This is an alias of {@link #controllers()} which* can be used for brevity if no other attributes are defined. See* {@link #controllers()} for details.* @see #controllers()* @return the controllers to test*/@AliasFor("controllers")Class<?>[] value() default {};/*** Specifies the controllers to test. May be left blank if all {@code @Controller}* beans should be added to the application context.* @see #value()* @return the controllers to test*/@AliasFor("value")Class<?>[] controllers() default {};/*** Determines if default filtering should be used with* {@link SpringBootApplication @SpringBootApplication}. By default only* {@code @Controller} (when no explicit {@link #controllers() controllers} are* defined), {@code @ControllerAdvice} and {@code WebMvcConfigurer} beans are* included.* @see #includeFilters()* @see #excludeFilters()* @return if default filters should be used*/boolean useDefaultFilters() default true;/*** A set of include filters which can be used to add otherwise filtered beans to the* application context.* @return include filters to apply*/Filter[] includeFilters() default {};/*** A set of exclude filters which can be used to filter beans that would otherwise be* added to the application context.* @return exclude filters to apply*/Filter[] excludeFilters() default {};/*** Auto-configuration exclusions that should be applied for this test.* @return auto-configuration exclusions to apply*/@AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")Class<?>[] excludeAutoConfiguration() default {};}
1、@WebMvcTest这个注解跟@SpringBootTest这个注解是不兼容的,从上面@WebMvcTest注解的定义来看,​​​​​​​@WebMvcTest这个注解支持指定controller的接口测试,这样就可以减少不必要组件的加载时间。

2、@WebMvcTest注解默认扫描 @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver这些组件,但是不会扫描 @Component, @Service or @Repository等组件,所以如果单独使用@WebMvcTest进行controller接口进行单元测试时,一般还要与@MockBean注解协同使用,下面给出一种用法:

@MockBean
private RemoteService remoteService;
@Autowired
private Reverser reverser;
@Test
void exampleTest() {
// 模拟RemoteService服务someCall接口调用返回值
given(this.remoteService.someCall()).willReturn("mock");
String reverse = reverser.reverseSomeCall();
assertThat(reverse).isEqualTo("kcom");
}

3、如果想使用数据库实际值进行测试,同时测试Service的相关接口流程,也可以使用@SpringBootTest+@AutoConfigureMockMvc注解的配合方式来完成。

总结,本文介绍了两种方式:

1、通过@WebMvcTest与@MockBean(Mock controller里面定义的Service接口)注解

2、通过@SpringBootTest与@AutoConfigureMockMvc注解实现

springboot进行controller单元测试相关推荐

  1. SpringBoot对Controller进行单元测试【含乱码解决】(详细代码)

    SpringBoot对Controller进行单元测试 Controller代码 单元测试代码 测试结果 乱码解决 Controller代码 package com.keafmd.controller ...

  2. java 单元测试_在springboot中写单元测试解决依赖注入和执行后事务回滚问题

    往期文章 「Java并发编程」谈谈Java中的内存模型JMM 面试官:说说你知道多少种线程池拒绝策略 为什么不要在MySQL中使用UTF-8编码方式 前言 很多公司都有写单元测试的硬性要求,在提交代码 ...

  3. springboot 事务嵌套问题_在springboot中写单元测试解决依赖注入和执行后事务回滚问题...

    往期文章 「Java并发编程」谈谈Java中的内存模型JMM 面试官:说说你知道多少种线程池拒绝策略 为什么不要在MySQL中使用UTF-8编码方式 前言 很多公司都有写单元测试的硬性要求,在提交代码 ...

  4. SpringBoot在controller返回一个HTML页面

    SpringBoot在controller返回一个HTML页面 本人今天在弄springBoot,因为第一次接触,遇到了很多的坑,特别是返回jsp页面.因为是新手所以一个人捣鼓了很长时间.终于弄好了. ...

  5. springboot对controller方法进行单元测试

    单元测试有助于验证程序的执行逻辑是否正确.controller层的单元测试,已经和接口测试很类似了.执行单元测试以前,需要添加测试依赖. <dependency><groupId&g ...

  6. springboot项目编写单元测试_SpringBoot项目单元测试(示例代码)

    前一段时间,有朋友问到springboot运用如何进行单元测试,结合LZ公司的实际运用,这里给大家描述一下三种单元测试的方式. 1.约定 单元测试代码写在src/test/java目录下 单元测试类命 ...

  7. Q1:spring-boot中Controller路径无法被访问的问题

    在学习spring-boot入门的第一个例子就是spring-boot-web的一个在页面上输出hello-world的例子,在运行这个例子的时候我遇到了下面这个简单的问题,但是第一次解决还是花了我很 ...

  8. 关于springboot项目(@SpringBootTest单元测试类)找不到配置文件问题

    配置类需要以下注解 @Component: 说明该类为配置类 @ConfigurationProperties(prefix = "test"): 用于获取配置文件内容,也可以使用 ...

  9. springboot使用junit单元测试是发生报错 Field taskUtils in com.xxx.xxx.xxxx.xxx required a bean of type 'xxx.xxx.

    使用junit单元测试mybatis时发生如下错误 Error starting ApplicationContext. To display the conditions report re-run ...

最新文章

  1. 从ICLR提交论文看机器学习的趋势和风口
  2. ComponentOne Wijmo Editor 在光标处添加文本
  3. JavaWeb学习笔记①——Java向下转型在JavaEE中运用——登陆验证
  4. python基础代码-python基础,python基础代码大全
  5. @RequestParam注解详解
  6. 如何配置html prettify,[HTML] Prettify 代码高亮使用总结
  7. MySQL 实用语句集合
  8. CodeForces 15B Laser
  9. vnc 树莓派 链接_树莓派 VNC Viewer 远程桌面配置教程
  10. nginx 代理静态资源报 403
  11. PL\SQL设置中文
  12. mysql 字节 字符_字符与字节 | 字痕随行
  13. 使用LTT升级HP磁带机的固件程序
  14. dts : rx8025t与lm75bd
  15. 亿能第二期测试沙龙-《企业自动化测试专题研讨会》4月中旬举行
  16. 计算机方面的缩写大全
  17. 银行卡号- 查询银行卡信息
  18. OS-练习题(10~13)
  19. Android学习笔记-传感器开发之利用传感器和Tween开发简易指南针
  20. R系银河麒麟配置本地镜像源

热门文章

  1. 全球与中国微电流美容仪器市场发展动态及前景战略研究预测报告2022-2028年
  2. No module named urls最新解决方法
  3. python 通过srcset属性获取最高像素图片
  4. 小程序开发过程中遇到过的那些坑
  5. iOS 新建本地数据库FMDB
  6. Unity3D Shader 入门第一天
  7. 摆摊到底挣不挣钱呢?都说摆摊好,只有自己开始摆摊了,才知道其中的不易
  8. c# 隐藏显示 任务栏
  9. jtdhrsgeagrshtd
  10. 个人网站支付,个人 APP 支付,如何接入支付?