{
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
BarConfigurationTest:
public class BarConfigurationTest {
private AnnotationConfigApplicationContext context;
@BeforeMethod
public void init() {
context = new AnnotationConfigApplicationContext();
}
@AfterMethod(alwaysRun = true)
public void reset() {
context.close();
}
@Test
public void testBarCreation() {
EnvironmentTestUtils.addEnvironment(context, "bar.name=test");
context.register(BarConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
context.refresh();
assertEquals(context.getBean(Bar.class).getName(), "test");
}
}
注意到因为我们使用了Configuration Properties机制,需要注册PropertyPlaceholderAutoConfiguration,否则在BarConfiguration里无法注入BarProperties。
参考文档