本文介绍了使用@ConextConfiguration在JUnit上测试具有不同构造函数参数的SpringBean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题描述:
我希望用户使用定制配置文件来配置Spring Bean,而不是Spring XML配置,如下所示:请注意,用户应该只配置字符串,所有其他Bean都应该在用户不知道的情况下@Autwire!
<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测试类?btw:我希望避免反射...
(下面的单元测试显示了我的意思。并且它们已经能够执行我想要的操作,但它们使用反射。)
我到目前为止所做的
之后使用反射来更改字段内容,但tbh根本不性感:
@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
}
}
推荐答案
有时提问有助于加大搜索难度。
@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
这篇关于使用@ConextConfiguration在JUnit上测试具有不同构造函数参数的SpringBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!