0

I have a multi module Spring Boot project with the following structure:

  • MyAppModule
    • src/main/java/Application.java
    • ...
  • IntegrationTests
    • src/test/java/integrationTest.java

The IntegrationTest module is testing the MyAppModule. The IntegrationTest module does not have a main package. Therefore there is no Spring Application. It has just the test package.

Nevertheless I would like to read in the application.yaml for some properties but I'm not able to because the attributes are always null:

@Configuration
@PropertySource("classpath:application.yaml")
public class IntegrationTest {

  @Value("${app.url}")
  private String appUrl;

}

Isn't it possible to use the @Value annotation without having a Spring Application (main with SpringApplication.run etc.)?

1
  • 1
    @PropertySource is only for .properties file not for loading .yaml files. Yes it is possible, no it isn't working (neither in Spring Boot) for YAML files. Commented Mar 19, 2018 at 8:22

3 Answers 3

1

Why not just @ConfigurationProperties?

@Configuration
@ConfigurationProperties(prefix = "app")
public class IntegrationTest {

 private String url;

 }
Sign up to request clarification or add additional context in comments.

Comments

0

Add Integration tests base package for component scanning in spring configuration.

@ComponentScan("basePackage1,basePackage2....etc")

Comments

0

Thanks for all responses. Solved it with the following code and a SpringBootApplication was necessary.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class IntegrationTest {

 @Value("${app.url}")
 private String url;

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.