Spring Boot
application properties
testing configuration
application-test.properties
Java development

Spring-boot application-test.properties

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Spring Boot, application-test.properties is the conventional place to keep configuration that should apply only when the test profile is active. This is useful for swapping databases, changing logging levels, or disabling integrations during automated tests. The file itself does nothing until the profile is activated, so the important part is understanding how profile activation and property precedence work together.

Where the File Belongs

For test-specific configuration, the usual location is:

text
src/test/resources/application-test.properties

Keeping it under src/test/resources ensures the file is available during tests without becoming part of the normal production artifact in the same way as main application resources.

You can also use application-test.yml if the project prefers YAML. The profile name and loading behavior are the same.

Activate the test Profile

Spring Boot only loads application-test.properties when the test profile is active.

A common test setup is:

java
1import org.springframework.boot.test.context.SpringBootTest;
2import org.springframework.test.context.ActiveProfiles;
3
4@SpringBootTest
5@ActiveProfiles("test")
6class UserServiceTest {
7}

With @ActiveProfiles("test"), Spring Boot loads the standard configuration and then overlays the test profile properties on top of it.

Use It to Override the Main Configuration

Suppose the main application uses a real database:

properties
1# src/main/resources/application.properties
2spring.datasource.url=jdbc:postgresql://prod-db/app
3spring.datasource.username=app
4spring.datasource.password=secret

A test profile can replace that with an in-memory database:

properties
1# src/test/resources/application-test.properties
2spring.datasource.url=jdbc:h2:mem:testdb
3spring.datasource.driver-class-name=org.h2.Driver
4spring.datasource.username=sa
5spring.datasource.password=
6spring.jpa.hibernate.ddl-auto=create-drop

This is the most common reason to use the file: tests should not hit production-like infrastructure by accident.

Understand Property Precedence

Spring Boot combines properties from many sources. In simplified form, later and more specific sources override earlier ones.

For typical tests, that means:

  • 'application.properties provides defaults'
  • 'application-test.properties overrides those defaults when the test profile is active'
  • annotations such as @TestPropertySource or inline test properties can override both

Example of an even more specific override:

java
1import org.springframework.boot.test.context.SpringBootTest;
2
3@SpringBootTest(properties = "feature.email.enabled=false")
4class EmailDisabledTest {
5}

That inline property beats whatever is defined in the profile file.

Use @TestPropertySource Only When Needed

If all tests should share the same profile configuration, application-test.properties plus @ActiveProfiles("test") is usually enough. @TestPropertySource is more useful when one test class needs a special override.

java
1import org.springframework.test.context.TestPropertySource;
2
3@TestPropertySource(properties = "external.api.base-url=http://localhost:9999")
4class ApiClientTest {
5}

This keeps one-off differences local instead of turning application-test.properties into a patchwork of unrelated cases.

Keep Test Configuration Focused

A good test properties file usually contains only things that genuinely differ from normal runtime configuration, such as:

  • in-memory database settings
  • test log levels
  • disabled schedulers or integrations
  • shorter timeouts

It should not become a second full production-style configuration file unless there is a real reason. The smaller it is, the easier it is to understand what the tests are intentionally changing.

Common Pitfalls

  • Creating application-test.properties but never activating the test profile.
  • Putting the file in the wrong resources directory so tests do not see it.
  • Duplicating the entire main configuration instead of overriding only what tests need.
  • Forgetting that inline test properties or @TestPropertySource can override profile values.
  • Letting tests hit real external services because the test profile was incomplete.

Summary

  • 'application-test.properties is the conventional Spring Boot configuration file for the test profile.'
  • It is typically stored in src/test/resources.
  • The file is only applied when the test profile is active, often through @ActiveProfiles("test").
  • Use it to override main configuration for safe, isolated test behavior.
  • Keep it focused and remember that more specific test property sources can still override it.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.