Main Tutorials

Spring @TestPropertySource example

TestPropertySource example

In Spring integration tests, we can use @TestPropertySource to override the application properties which default loaded from application.properties or application.yml.

Table of contents

P.S Tested with Spring Boot 3.1.2

1. Service Class

1.1 We have a service class to get the values from the application.properties.

application.properties

# Database
db.name=mkyong
db.thread-pool=5
DatabaseService.java

package com.mkyong.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class DatabaseService {

  @Value("${db.name:hello}")
  private String name;

  @Value("${db.thread-pool:3}")
  private Integer threadPool;

  //getters, setters and etc.
}

1.2 Starts the Spring Boot application.

Application.java

package com.mkyong;

import com.mkyong.service.DatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private DatabaseService dbConfig;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(dbConfig);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Output

Terminal

DatabaseService{name='mkyong', threadPool=5}

2. Test with the @TestPropertySource

The @SpringBootTest loads the property values from the following sequence:

  1. Properties from @TestPropertySource
  2. The application.properties or application.yml from the src/test/resources directory.
  3. The application.properties or application.yml from the src/main/resources directory.

Review the project structure.

test environment

Spring loads the property values from the src/main/resources/application.properties in this case.

application.properties

# Database
db.name=mkyong
db.thread-pool=5

We create a new src/test/resources/test.properties with new thread pool values for the tests.

test.properties

db.thread-pool=10

And we use @TestPropertySource to load the test.properties file and override the thread pool values from the main application’s property value.

ApplicationTest2.java

package com.mkyong;

import com.mkyong.service.DatabaseService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
@TestPropertySource("classpath:test.properties")
public class ApplicationTest2 {

  @Autowired
  private DatabaseService dbServer;

  // This value from the application.properties.
  @Test
  public void testDatabaseName() {
      assertEquals("mkyong", dbServer.getName());
  }

  // We expect 10 from the test.properties; the original application.properties is 5.
  @Test
  public void testDatabaseThreadPool() {
      assertEquals(10, dbServer.getThreadPool());
  }

}

3. @TestPropertySource Inline Properties

The @TestPropertySource supports inline properties directly.


// inline properties
@TestPropertySource(properties = {"db.thread-pool=10"})  

We can also defined multiple inline properties directly,


//multiple inline properties
@TestPropertySource(properties = {
      "db.thread-pool=10",
      "db.name=mkyong"
})

4. Download Source Code

$ git clone https://github.com/mkyong/spring-boot.git

$ cd spring-boot-externalize-config-4

$ mvn test

$ mvn spring-boot:run

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Div
2 months ago

application.yml is not supported