Spring Boot @TestConfiguration Example

Learn to create and import test configurations in spring boot applications with the help of @TestConfiguration annotation.

1. Creating @TestConfiguration

The @TestConfiguration is a specialized form of @Configuration annotation that can be used to define additional beans or customizations specific to tests.

@TestConfiguration
public class AppTestConfiguration {

    @Bean
    DataSource inMemoryDataSource(){
    	//...
    }
}

2. Importing Test Configurations

In spring boot, any beans configured in a top-level class annotated with @TestConfiguration will not be picked up via component scanning. We must explicitly register the @TestConfiguration class with the class that contains the test cases.

There are two ways to include the additional test configuration for tests:

2.1. Using @Import

The @Import annotation indicates one or more configuration classes to import into the application context or spring test context. This annotation should be declared at the class level or as a meta-annotation.

@Import(AppTestConfiguration.class)
//other annotations
public class AppTests
{
	...
}

The @Bean definitions declared in imported @TestConfiguration classes should be accessed by using @Autowired injection. Either the bean itself can be autowired, or the configuration class instance declaring the bean can be autowired.

@Import(AppTestConfiguration.class)
public class AppTests
{
    @Autowired
    DataSource datasource;

    //tests
}

If XML or other non-configuration bean definition resources need to be imported, use the @ImportResource annotation instead.

2.2. Using Static Nested @TestConfiguration Class

We can define the test configurations in nested classes inside the test class. The nested class can be annotated with @Configuration or @TestConfiguration annotations.

If the class annotated with @TestConfiguration or @Configuration is a static nested class within the test class, it would be registered automatically.

  • In the case of nested @Configuration class, the given configuration would be used “instead of” the application’s primary configuration.
  • A nested @TestConfiguration class is used “in addition to” the application’s primary configuration.
public class AppTests
{
    @Autowired
    DataSource datasource;

    //tests

    @TestConfiguration
		static class AppTestConfiguration {

		    @Bean
		    DataSource inMemoryDataSource(){
		    	//
		    }
		}
}

3. Conclusion

The @TestConfiguration annotation is a very useful way to provide test-specific configurations and beans while performing unit testing and integration testing.

The best thing is that these test configurations are not automatically part of the application’s primary configuration. These are available only on-demand using either @Import annotation or static nested classes.

Beware of the difference between @Configutation and @TestConfiguration annotations in nested classes as the first excludes the primary configuration while the latter is used to create additional test configuration in addition to the primary configuration.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.