@MockBean 注解后 bean成员对象为 null?
笔者在写自测的时候遇到的问题:
我想模拟一个Bean,并在之后使用Mockito打桩,于是使用了 @MockBean 注解(spring集成mockito的产物),但代码编写好了后启动测试却报NullPointerException
好家伙,bean没有Mock上。
交代一下我的代码背景:
框架:SpringBoot、SpringBoot Test、TestNG、Mockito
@SpringBootTest
@TestPropertySource("classpath:application.properties")
@TestExecutionListeners(MockitoTestExecutionListener.class)
public class XXXServiceTest extends AbstractTestNGSpringContextTests {
@MockBean
XXXComponent xxxComponent; // null ???
}
解决办法
// 在测试类上加
@TestExecutionListeners(MockitoTestExecutionListener.class)
原因
因为Spring集成TestNG的抽象类: AbstractTestNGSpringContextTests
上并没加关于Mockito相关的测试监听器,所以就没有运用上MockBean的注解功能,看源码
@TestExecutionListeners({ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
public abstract class AbstractTestNGSpringContextTests implements IHookable, ApplicationContextAware {
PS:官方也有讨论过这个issue,但官方并不打算集成进来,所以叫大家自己加个Listener则是,我认为也是该这样,因为Mockito这样的产品会有很多种。
@MockBean 注解后 bean成员对象为 null?的更多相关文章
- Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等
实验1:配置通过静态工厂方法创建的bean [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...
- Spring(十五):通过注解配置 Bean
在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...
- Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】
通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XML 的功能,也提供了自动装配的功能,采用了 ...
- (转)Spring注解完成Bean的定义
使用Spring注解完成Bean的定义 2010-04-21 16:48:54| 分类: spring|举报|字号 订阅 下载LOFTER客户端 通过@Autowired或@Reso ...
- 5.spring:注解配置 Bean
在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)
Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性) 前言:由于前段时间忙于写接口,在接口中需要做很多的参数校验,本着简洁.高效的原则,便写了这个小工具供自己使用(内容 ...
- 在Servlet中获取Spring注解的bean
最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...
- Spring IOC机制之使用注解配置bean
一. 通过注解配置bean 1.1 概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2 ...
随机推荐
- ABP设置管理模块: Abp.SettingUi
开源地址: https://github.com/EasyAbp/Abp.SettingUi 一直想宣传一下SettingUi, 因为 懒 工作比较忙, 所以才拖到今天. 关于ABP就不需要我再多废口 ...
- ElasticSearch 交互使用
Curl 命令 # 建立索引 [root@dbtest01 ~]# curl -XPUT 'http://10.0.0.121:9200/test' # 插入数据 [root@dbtest01 ~]# ...
- 网络安全-WEB基础,burpsuite,WEB漏洞
1. web基础 HTTP: GET POST REQUEST RESPONSE... JDK robots.txt 网页源代码/注释 目录扫描--御剑,dirmap 端口信息--nmap 备份文件- ...
- np.random.randint()的返回值
返回的是数组而非int 比如返回x,y 为[1][2] 而非1,2 容易在只有一维一列时没有意识到 其他函数的返回值也要注意
- python 表达式
运算符 参考 https://www.runoob.com/python3/python3-basic-operators.html & https://www.runoob.com/pyth ...
- nyoj-1236 挑战密室
挑战密室 时间限制:1 s | 内存限制:128 M 提交 状态 排名 题目描述 R组织的特工Dr. Kong 为了寻找丢失的超体元素,不幸陷入WTO密室.Dr. Kong必须尽快找到解锁密码逃离,否 ...
- Leetcode(28)-实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- React Creating Elements All In One
React Creating Elements All In One https://reactjs.org/docs/react-api.html#creating-react-elements h ...
- Linux directory tree
Linux directory tree https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard https://en.wikipedi ...
- Learning web development with MDN
Learning web development with MDN Server-side website programming Dynamic Websites – Server-side pro ...