Why are properties from appliction.properties not available in Junit test in spring boot application?

I have a spring boot application. I am writing Junit tests. I am trying to enter values ​​from application.properties (defined in src / main / resources / application.properties) and Status bean configured in AppConfig (src / main / java / hello / AppConfig.java). I see that the bean is autwired (through the debugger it is not null), but the values ​​are not set.

Here is the application.properties

Src / core / resources / application.propertie s

app.name=rest-service app.version=1.0.0-SNAPSHOT 

Src / Main / Java / Hello / AppConfig.java

 @Configuration public class AppConfig { @Value("${app.version}") private String version; @Value("${app.name}") private String name; @Bean public Status status(){ return new Status(name,version); } } 

// Status is a simple pojo with name and version fields

Src / test / java / hello / GreetingControllerTests.java

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @ContextConfiguration(classes={hello.TestConfig.class,hello.AppConfig.class}) @WebAppConfiguration public class GreetingControllerTests { @Autowired Environment envi; @Autowired Status status; @Value("${app.name:notConfigured}") String appName; private String env; @Before public void init(){ System.out.println(status.getName()); System.out.println(appName); env=envi.getActiveProfiles()[0]; } @Test public void should_fail(){ if (env.equalsIgnoreCase("DEV")){ assertThat(false).isFalse(); } if (env.equalsIgnoreCase("QA1")){ System.out.println("failing the test"); fail(); } } } 

I install the debugger in the init method and find that both the appName and the status bean with the right value sets are NOT inserted. The bean status is entered. just the values ​​are not set.

+5
source share
2 answers

Thank you for sharing the project. It was much easier to work with him. Please see my craving request (with comments) to understand how I work all this.

https://bitbucket.org/SpringDevSeattle/gs-rest-service/pull-requests/4/updating-project-for-spring-boot-14/diff

* Update * You had annotations that were overwritten by mixing old Spring annotations with new annotations. Credits for spring-boot properties are not @Autowired to indicate a solution based on a Google search application.properties not being populated in the environment .

If you look @SpringApplicationConfiguration :

 @ContextConfiguration(loader = SpringApplicationContextLoader.class) @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Deprecated public @interface SpringApplicationConfiguration { 

In fact, it consists of several other annotations. Important is @ContextConfiguration . For @SpringApplicationConfiguration it uses the loader and this loader scans the class path for the corresponding configuration files along with the loaded components (one of them is that it reads application.properties in the environment). As part of the test, you override this annotation with your ad:

 @ContextConfiguration(classes={hello.TestConfig.class,hello.AppConfig.class}) 

This causes Spring to load the TestConfig and AppConfig classes without scanning other configuration files. Commenting that the annotation and its launch will force to pass the test and debug the init () method, the entered values ​​will be displayed.

+4
source

You need another properties file in the resource directory under your tests.

 /src/test/resources 

What the unit is looking for.

0
source

All Articles