SpringBoot @MockBean 导致ApplicationContext Reload带来的问题的解决方法

在基于SpringBoot的项目中,编写单元测试时,会遇到需要对一些被Spring容器管理的对象进行Mock的处理,但是这些对象可能被引用的比较多。这个时候可以使用 @MockBean 来注释相关对象。

如下面的代码片段:

package com.example.springbootdemo;

import com.example.springbootdemo.service.FillDataService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

@SpringBootTest
public class TestControllerTest {
    @MockBean
    private FillDataService fillDataService;

    @Test
    public void testAny() {
        System.out.println("abc");
    }
}

但使用 @MockBean 的同时,可能会带来一些隐藏的问题。因为 @MockBean 会导致 Spring的 ApplicationContext 进行多次Reload,项目中或者其他依赖的框架中,针对多次Reload的情况可能会出现异常。导致单元测试无法正常进行下去。

网上找了很久解决方案,找到了一种方案。在此记录以防以后碰到。

解决方案:
1、使用 Mockito@Mock 注解,(但是该注解mock出来的对象是没有直接被容器管理的,也就是说Spring容器中某些对象依赖这种类型的对象不是mock的。)
2、将Spring容器中与Mock对象相同类型的对象进行替换,替换为Mock对象。

代码实现可参考以下:

    @BeforeEach
    public void before() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        GenericApplicationContext context = (GenericApplicationContext) this.webApplicationContext;
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
        replaceBean("cafJdbcTemplate", cafJdbcTemplate, beanFactory);
        replaceBean("holoJdbcTemplate", holoJdbcTemplate, beanFactory);
        replaceBean("cafNamedParameterJdbcTemplate", cafNamedParameterJdbcTemplate, beanFactory);
        replaceBean("holoNamedParameterJdbcTemplate", holoNamedParameterJdbcTemplate, beanFactory);
    }

    public void replaceBean(String beanName, Object o, DefaultListableBeanFactory beanFactory) {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
        beanFactory.removeBeanDefinition(beanName);
        beanDefinition.setBeanClassName(o.getClass().getCanonicalName());
        beanFactory.registerBeanDefinition(beanName, beanDefinition);
        beanFactory.registerSingleton(beanName, o);
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值