Spring boot – @TestConfiguration

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

1. @TestConfiguration annotations

@TestConfiguration is specialized form of @Configuration that can be used to define additional beans or customizations for a test.

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 this additional test configuration for tests:

1.1. @Import annotation

It indicates one or more configuration classes to import into application context or spring test context.

@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.

It should be declared at the class level or as a meta-annotation.

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

@TestConfiguration
public class MyTestConfiguration {

    //tests specific beans
    @Bean
    DataSource createDataSource(){
    	//
    }
}
@Import(MyTestConfiguration.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SpringBootDemoApplicationTests 
{   
    @LocalServerPort
    int randomServerPort;

    @Autowired
    DataSource datasource;

    //tests
}

1.2. Static nested classes

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 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.
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SpringBootDemoApplicationTests 
{   
    @LocalServerPort
    int randomServerPort;

    @Autowired
    DataSource datasource;

    //tests

    @TestConfiguration
	static class MyTestConfiguration {

	    //tests specific beans
	    @Bean
	    DataSource createDataSource(){
	    	//
	    }
	}
}

2. Conclusion

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

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

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

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.