Define spring property values in Java

雨燕双飞 提交于 2019-12-11 08:59:47

问题


I have some spring beans which wire in property values using the @Value annotation.

e.g.

@Value("${my.property}")
private String myField;

Usually the values are sourced from property files.

The test I am currently writing uses a fully annotation based configuration.

e.g.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class AcceptanceTest implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Configuration
    @ComponentScan(basePackages = {
        "my.package.one",
        "my.package.two"
    })
    static class ContextConfiguration {
        @Bean
        public MyBean getMyBean(){
            return new MyBean();
        }
    }

    @Autowired
    private AnotherBean anotherBean;

    @Test
    public void testTest(){
        assertNotNull(anotherBean);
        . . .
    }   
    . . .

I don't wish to reference an external properties file, as I want to keep everything local to the test.

Is there anyway I can specify in java, values for such properties, so that they will be wired in automatically to any beans which need them.

Any help would be appreciated.


回答1:


As of Spring Framework 4.1, you can use the @TestPropertySource annotation to declare inlined properties for the ApplicationContext loaded for your tests like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestPropertySource(properties = { "foo = bar", "magicNumber: 42" })
public class ExampleTests { /* ... */ }

Consult the Context configuration with test property sources section of the reference manual for details.


Prior to Spring Framework 4.1, the easiest way is to configure a custom PropertySource and register it with the Spring TestContext Framework (before the ApplicationContext is loaded for your test).

You can achieve this by implementing a custom ApplicationContextInitializer and using an org.springframework.mock.env.MockPropertySource like this:

public class PropertySourceInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    public void initialize(ConfigurableApplicationContext applicationContext) {
         applicationContext.getEnvironment().getPropertySources().addFirst(
           new MockPropertySource().withProperty("foo", "bar"));
    }
}

You can then register your initializer for your test like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = PropertySourceInitializer.class)
public class ExampleTests { /* ... */ }

Regards,

Sam (author of the Spring TestContext Framework)




回答2:


Here's one simple approach:

@Configuration
public class PropertiesConfig {

    @Bean
    public PropertyPlaceholderConfigurer myConfigurer() {
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        Properties props = new Properties();
        Map myMap = new HashMap<String, String>();

        myMap.put("my.property", "my value");
        myMap.put("second.my.property", "another value");
        props.putAll(myMap);

        configurer.setProperties(props);
        return configurer;
    }
}



回答3:


If you are using Spock you can also use @TestPropertySource:

@SpringBootTest
@TestPropertySource(properties = [ "my.test.property = bar", "..." ])

It requires the String array to be in Groovy syntax of course, caught me out. I'm using Spock 1.1



来源:https://stackoverflow.com/questions/23820171/define-spring-property-values-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!