0

I have a spring boot application and 'mvn test' is failing because Autowired is not working for reading value from application.yml file.

Class file:

@Configuration
public class LogConfig {

    private SettingsRepository settingsRepository;

    @Value("${build.version}")
    private String buildVersion;

    @Value("${build.name}")
    private String buildName;

    public LogConfig(SettingsRepository settingsRepository) {
        this.settingsRepository = settingsRepository;
    }

    @Scheduled(fixedDelay = 5000)
    public void injectDataForLogging() throws SocketException {
        MDC.put("applicationToken", settingsRepository.getToken().isPresent() ? settingsRepository.getToken().get() : "NO-TOKEN");
        MDC.put("macAddressList", MACAddressUtil.getMACAddresses().toString());
        MDC.put("application", buildName);
        MDC.put("applicationVersion", buildVersion);
    }
}

application.yml:

build:
  version: @project.version@
  name: @project.artifactId@

pom.xml:

<artifactId>something</artifactId>
<version>2.0.0</version>

Error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'build.version' in value "${build.version}"

When I run the application, everything works normally :/

SasaFajkovic
  • 395
  • 1
  • 4
  • 16
  • There is no test class here. What is the test class that's failing? – Christopher Schneider Mar 30 '20 at 21:09
  • 1
    Does this answer your question? [Populating Spring @Value during Unit Test](https://stackoverflow.com/questions/17353327/populating-spring-value-during-unit-test) – Ahmed Maher Mar 30 '20 at 21:14
  • possible duplicate of :https://stackoverflow.com/questions/17353327/populating-spring-value-during-unit-test – Ahmed Maher Mar 30 '20 at 21:15
  • Boot already provides this info for you in `BuildProperties`. (And why are you changing your MDC repeatedly when that info shouldn't change? And avoid `isPresent` whenever possible; instead, `getToken().orElse("NO-TOKEN")`.) – chrylis -cautiouslyoptimistic- Mar 30 '20 at 22:16
  • @ChristopherSchneider - the test class isn't the problem. It fails when loading the context while trying to execute tests. I have around 80 tests and all that are dependent on the context are failing. – SasaFajkovic Mar 31 '20 at 11:34
  • @chrylis-onstrike- - I've corrected to use orElse(). Thanks. I need to update the application token only since there is a scenario when you firts load the application (client-side) you don't have the token and you need to enter it. Once you save it, I need to update the MDC. I'll fix later how this works since it is still in the POC phase. – SasaFajkovic Mar 31 '20 at 11:36

2 Answers2

0

Just as you pass pom.xml properties to your main application.yml as it follows:

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

You will need to do the same to pass those properties to your test resources:

 <build>
    <testResources>
       <testResource>
          <directory>src/test/resources</directory>
              <filtering>true</filtering>
          </testResource>
    </testResources>
 </build>
DaviM
  • 324
  • 2
  • 7
0

In the end, I went with using Spring's Build Properties bean.

Still not sure why this wasn't working but have no time to play with it anymore :D

SasaFajkovic
  • 395
  • 1
  • 4
  • 16