0

I tried a spring boot 2.4.0 application, wrote some tests. Here's my test class

@SpringBootTest
@ActiveProfiles("test")
@TestPropertySource(locations = "classpath:application-test.properties")
public class SampleTest {
    @Test
    public void testMethod1() {
        //some logic
    }
}

I have this structure

src/
  main/
    java/
      //// further packages
    resources/
      bootstrap.yml
  test/
    resources/
      application-test.properties

Above code is picking bootstrap.yml as it contains this property spring.profiles.active=${PROFILE} Btw, this application is using spring-cloud-config

It gives this error

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'PROFILE' in value "${PROFILE}"

Why is spring-boot not picking up my test properties file? It is giving precedence to bootstrap.yml file always. Please help

3
  • Remove TestPropertySource and EnableConfigurationProperties Commented Mar 22, 2021 at 16:02
  • Try the same name and extension for test properties. I mean change application-test.properties to application.yml in src/test/resource Commented Mar 23, 2021 at 4:08
  • @SimonMartinelli tried EnableConfigurationProperties, didnt work Commented Mar 23, 2021 at 5:51

2 Answers 2

1

I reproduced your example and it works if I just add this to the test class

@ActiveProfiles("test")
@SpringBootTest
class SampleTest {

You don't need any of the other annotations.

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

8 Comments

and you have retained the same structure of test and main properties?
this still didnt work. I also have spring-kafka-test dependency in my project. could that be interfering in some way?
Compare your project with mine: github.com/simasch/demo-profiles
I had made a mistake in my post. There's bootstrap.yml instead of application.yml in my project. Now a strange observation, if I change name from bootstrap to application then everything worked fine. Test class picked src/test/resources/application-test.properties file!
Sure because bootstrap.yaml is for Spring Cloud and will be picked up always
|
0

I solved this myself with a little help from @SimonMartineli. My spring-boot 2.4.0 project uses spring-cloud-config. Hence there is bootstrap.yml. It has two properties

spring.active.profiles=${PROFILE}
spring.cloud.config.uri=${CONFIG_SERVER_URI}

These placeholders are supplied values from env variables in run-time.

Now even if I mention @TestPropertySource(locations = "classpath:application-test.properties") in my test classes, it doesnt seem to be working. Test class still tries to load bootstrap.yml fist. It used to work in spring-boot 2.1.5 from which I migrated.

To solve this I used this annotation in my test class.

@SpringBootTest(properties = {"spring.cloud.config.enabled=false", "spring.profiles.active=test"}) 
public class SampleTest {
    @Test
    public void testMethod1() {
        //some logic
    }
}

So this basically solved the other property that spring-cloud-config looks for which is that spring.profiles.active and it had a placeholder. Hope this helps someone who faces similar issue.

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.