使用@ContextConfiguration在JUnit上测试具有不同构造函数args的Spring bean

时间:2019-03-15 13:18:05

标签: java spring junit4

问题描述:


我希望用户通过spring XML配置使用自定义配置文件配置spring bean,如下所示: 请注意,用户只能配置字符串,其他所有bean应该在用户不知道的情况下进行@Autowired!

<bean class="com.my.group.Provider">
    <constructor-arg value="config1.proprietary"/>
    <constructor-arg value="config2.proprietary"/>
</bean>

Provider对象的外观(简化)如下:

public class Provider {
    @Autowired
    private Foo foo;
    private final String[] configNames;

    public Provider(final String... configs) {
        this.configNames = Preconditions.checkNotNull(configs, "Provided configs must not be null!");
    }

    public List<Configs> getConfigs() {
         return new foo.create(configNames); // here is more logic that I would actually like to test... (not just methods called on foo)
    }
}

我的问题是:

我如何使用各种不同的字符串输入来测试此解决方案,以便所有测试都可以进入一个JUnit Test类? Btw:我想避免反射...

(下面的单元测试显示了我的意思。它们已经能够执行我想要的操作,但是它们使用了反射。)


我到目前为止所做的

随后使用反射来更改字段内容,但一点都不是性感

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ProviderTest.MyContext.class})
public class ProviderTest {

    @Autowired
    private Provider sut;

    @Test
    public void provide_oneConfig() throws NoSuchFieldException, IllegalAccessException {
        setConfigFilesViaReflection(sut, "config1.proprietary"");
        // When
        List<Config> configs = sut.getConfigs();

        // Then
        assertEquals(1, configs.size());
    }

    @Test
    public void provide_twoConfigs() throws NoSuchFieldException, IllegalAccessException {
        setConfigFilesViaReflection(sut, "config1.proprietary", config2.proprietary");
        // When
        List<Config> configs = sut.getConfigs();

        // Then
        assertEquals(2, configs.size());
    }

    private void setConfigFilesViaReflection(final Provider sut, final String... configs) throws NoSuchFieldException,
            IllegalAccessException {
        Field configNamesField = Provider.class.getDeclaredField("configNames");
        configNamesField.setAccessible(true);
        configNamesField.set(sut, configs);
    }

    @Configuration
    public static class MyContext {
        @Bean
        Provider provider() {
            return new Provider("willBeOverridenByReflection");
        }

        @Bean
        Foo foo() {
            return new Foo(); // this one got mocked in my test
        }
}

1 个答案:

答案 0 :(得分:1)

有时候问一个问题有助于更努力地搜索。

使用@Qualifier / @Resource批注可以创建多个bean,并按如下所示在每个测试中选择它们:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ProviderTest.MyContext.class})
public class ProviderTest {

    @Autowired
    @Qualifier("bar") // could also be @Resource (without @Autowired)
    private Provider sut;
    @Resource(name="baz")
    private Provider sut2; // could also be @Qualifier(with @Autowired)

    @Test
    public void provide_oneConfig() throws NoSuchFieldException, IllegalAccessException {
        // When
        List<Config> configs = sut.getConfigs();

        // Then
        assertEquals(1, configs.size());
    }

    @Test
    public void provide_twoConfigs() throws NoSuchFieldException, IllegalAccessException {
        // When
        List<Config> configs = sut2.getConfigs();

        // Then
        assertEquals(2, configs.size());
    }

    @Configuration
    public static class MyContext {
        @Bean("bar")
        Provider providerBar() {
            return new Provider"config1.proprietary");
        }
        @Bean("baz")
        Provider providerBaz() {
            return new Provider("config1.proprietary", "config2.proprietary");
        }

        @Bean
        Foo foo() {
            return new Foo(); // this one got mocked in my test
        }
}

在这里找到我的答案:Autowiring two different beans of same class

相关问题