http://stackoverflow.com/questions/11306951/how-to-set-environment-variable-or-system-property-in-spring-tests
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Value annotations working on spring junit test, annotation ONLY config. no xml Raw
@Configuration
@ComponentScan("dan.langford")
static class someConfig {
// because @PropertySource doesnt work in annotation only land
@Bean
PropertyPlaceholderConfigurer propConfig() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("some.properties"));
return ppc;
}
}
http://fahdshariff.blogspot.com/2012/09/spring-3-javaconfig-loading-properties.html
@PropertySource("classpath:${APP_ENV:default}.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext {
static {
System.setProperty("myproperty", "foo");
}
}
https://gist.github.com/danlangford/3418696@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Value annotations working on spring junit test, annotation ONLY config. no xml Raw
@Configuration
@ComponentScan("dan.langford")
static class someConfig {
// because @PropertySource doesnt work in annotation only land
@Bean
PropertyPlaceholderConfigurer propConfig() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("some.properties"));
return ppc;
}
}
http://fahdshariff.blogspot.com/2012/09/spring-3-javaconfig-loading-properties.html
@PropertySource("classpath:${APP_ENV:default}.properties")
http://stackoverflow.com/questions/32633638/testpropertsource-and-propertysource-dont-work-for-junit
Your use of @Value requires a PropertySourcesPlaceholderConfigurer bean to resolve ${...}
placeholders. @Configuration
public class PropertyTestConfiguration {
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(ArrayUtils.addAll(
new PathMatchingResourcePatternResolver().getResources("classpath*:application.properties"),
new PathMatchingResourcePatternResolver().getResources("classpath*:test.properties")
)
);
return ppc;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class PropertyTests {
@Value("${elastic.index}")
String index;
@Configuration
@Import({PropertyTestConfiguration.class})
static class ContextConfiguration {
}
}