在 Spring 上重新注册单例 bean

min*_*das 5 java spring unit-testing mocking

我有一个多模块项目,其中每个模块都有自己的单元测试,并为该模块的类提供模拟。

我正在尝试构建一个应用程序上下文,其中每个模块都可以定义自己的模拟,但以后的单元测试将能够覆盖这些模拟,例如:

public class Test {

    private static final class StupidMock {
    }

    @org.junit.Test
    public void test() {
        StaticApplicationContext applicationContext = new StaticApplicationContext();
        final ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        StupidMock stupidMock = new StupidMock();  // original mock
        beanFactory.registerSingleton(StupidMock.class.getName(), stupidMock);

        StupidMock f1 = applicationContext.getBean(StupidMock.class);
        if (f1 == null || f1 != stupidMock) {  // ensuring mock is retrievable
            fail("Could not get bean");
        }

        for (String names2Remove : beanFactory.getBeanNamesForType(StupidMock.class)) {
            applicationContext.removeBeanDefinition(names2Remove);  // <-- fails here 
        }

        StupidMock stupidMock2 = new StupidMock();     // replacement mock
        beanFactory.registerSingleton(StupidMock.class.getName(), stupidMock2);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是这个简单的片段在尝试删除第一个模拟时失败,声称没有这样的 bean(尽管 Spring 刚刚成功地为我提供了一个名称)。

如果我只是尝试在第一个模拟之上注册另一个模拟,Spring 会抱怨说已经绑定了对象。

DefaultSingletonBeanRegistry有一个removeSingleton受保护的方法,但我无法控制StaticApplicationContext. 我可能会使用反射并调用这个受保护的方法,但是对于这样一个简单的任务来说这样做是错误的。

我究竟做错了什么?我怎样才能在 上实现单例替换StaticApplicationContext

Bij*_*men 3

这里的问题是, registerSingleton 方法实际上并没有创建相应的BeanDefinition,它只是注册实例化的单例,将其与您提供的名称关联起来,并稍后通过应用程序上下文检索它 - 但它背后没有 BeanDefinition 。

因此,当您调用时,applicationContext.removeBeanDefinition(names2Remove);它会失败,因为没有bean 定义,只有注册的完全实例化的 bean。

解决方法是不使用 registerSingleton,而是使用使用 BeanDefinition 的 registerSingleton 形式:

Map<String, String> map = new HashMap<String, String>();
map.put("i", "10"); // set all the properties for the mock..
MutablePropertyValues propertyValues = new MutablePropertyValues(map);

beanFactory.registerSingleton(StupidMock.class.getName(), StupidMock.class, propertyValues);
Run Code Online (Sandbox Code Playgroud)