0

Not sure what I'm doing wrong here, but I have the following value that points to the URL of a micro service.

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

And I am setting up my dev profile in my application.yml file such:

---
spring:
  datasource:
    url: jdbc:h2:mem:test
    username: sa
    password: sa
    driverClassName: org.h2.Driver
  config:
    activate:
      on-profile: dev

mpesa:
  host: sandbox.safaricom.co.ke
  port: 443

url:
  dispenseRoot: https://localhost:8844/dispense
---

Then, in accordance with my answer here: How do I activate a Spring Boot profile when running from IntelliJ?, I set up the spring boot profile on the run configuration as such:

enter image description here

And then run the following test to test my configuration:

@SpringBootTest
public class PaymentApplicationTests {

    @Test
    public void contextLoads() {
    }

}

And I get:

    ... 111 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'url.dispenseRoot' in value "${url.dispenseRoot}"

Now, if I copy and paste the following lines:

url:
  dispenseRoot: https://localhost:8844/dispense

Above my profile where I'm setting up the general settings that are true for everything, it works fine. Therefore, I'm certain it has something to do with profiles, but I can't figure out what it is.

Please advise...

Thom
  • 14,013
  • 25
  • 105
  • 185
  • 1
    The file name of your properties is wrong. It should be application-dev.yml – Ozay Duman Aug 11 '21 at 09:00
  • could you clarify what you mean with "Above my profile where I'm setting up the general settings that are true for everything" ? – Tomas F Aug 11 '21 at 09:15
  • Your tests are probably run with a test-profile, meaning you need a separate properties-file in your `test/resources` dir. Be warned that the `@Value` annotation doesn't work for unit tests, so if you need to populate the field you have to do it via reflection. – Erik Karlstrand Aug 11 '21 at 10:00

0 Answers0