Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfigFileApplicationContextInitializer, JUnit and YAML file #6120

Closed
ctranxuan opened this issue Jun 6, 2016 · 2 comments
Closed

ConfigFileApplicationContextInitializer, JUnit and YAML file #6120

ctranxuan opened this issue Jun 6, 2016 · 2 comments
Labels
type: documentation A documentation update
Milestone

Comments

@ctranxuan
Copy link

ctranxuan commented Jun 6, 2016

Hi,
I am trying to test a subset of a SpringBoot app with JUnit. As stated in the documentation (here), we should be able to use ConfigFileApplicationContextInitializer.

In a JUnit test-case, I am trying to inject a some @Value properties from a application-dev.yml file (e.g. @Value("${my.pojo.name}") String name;). But these values are not injected (I've got the String "${my.pojo.name}" injected) when using ConfigFileApplicationContextInitializer and the annotation @ContextConfiguration while it seems to work when using @SpringApplicationConfiguration with a SpringApplication.

Context
Given a configuration class and a JUnit class:

@Configuration
public class PojoConfiguration {
    @Value("${my.pojo.name}")
    public String name;

  @Bean
    public Pojo pojo() {
        return new Pojo(name);
    }
 }

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("dev") 
@ContextConfiguration(classes = { PojoConfiguration.class }, initializers = ConfigFileApplicationContextInitializer.class)
public class PojoTest {
    @Autowired
    private Pojo myPojo;

    @Test
    public void testValue() {
        Assert.assertEquals(myPojo.getName(), "Arthur Accroc");
    }
}

and a application-dev.yml:

my:
    pojo:
        name: Arthur Accroc

Obtained: PojoConfiguration.name has the value ${my.pojo.name}
Expected: PojoConfiguration.name has the value defined in the application-dev.yml

You can reproduce the issue with the demo.tar.gz in attachment.

I've tested with SpringBoot version 1.2.7.RELEASE and 1.3.5.RELEASE.

It looks like other people have this issue (here)

Thanks in advance,

Cédric.

demo.tar.gz

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 6, 2016
@wilkinsona
Copy link
Member

There's no PropertySourcesPlaceholderConfigurer in your test configuration so the placeholder remains unresolved. You need to add some configuration that publishes one as a bean. If you use @SpringApplicationConfiguration then one will typically be auto-configured for you.

We should improve the docs.

@wilkinsona wilkinsona added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 15, 2016
@wilkinsona wilkinsona added this to the 1.4.0 milestone Jul 15, 2016
@ctranxuan
Copy link
Author

Thanks for the response!
Indeed a note in the doc is a great idea. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants