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

发布于:2024-05-02 ⋅ 阅读:(144) ⋅ 点赞:(0)

在基于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);
    }

网站公告

今日签到

点亮在社区的每一天
去签到