Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override a single @Configuration class on every spring boot @Test

On my spring boot application I want to override just one of my @Configuration classes with a test configuration (in particular my @EnableAuthorizationServer @Configuration class), on all of my tests.

So far after an overview of spring boot testing features and spring integration testing features no straightforward solution has surfaced:

  • @TestConfiguration: It's for extending, not overriding;
  • @ContextConfiguration(classes=…​) and @SpringApplicationConfiguration(classes =…​) let me override the whole config, not just the one class;
  • An inner @Configuration class inside a @Test is suggested to override the default configuration, but no example is provided;

Any suggestions?

like image 259
NotGaeL Avatar asked Aug 19 '16 14:08

NotGaeL


People also ask

How do I override a configuration class in spring boot?

To make a configuration in Spring Boot, you need to create a class and annotate it with @Configuration . Usually, in the configuration class, you can define a beans object. But if you want to override built-in configuration, you need to create a new class that extends the built-in class.

What does @configuration annotation do in spring boot?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

Can we have multiple @configuration in spring?

You should be able to autowire them: @Configuration public class Conf2 { @Autowired Conf1 conf1; ... } Alternatively, you can autowire beans rather than configurations: @Configuration public class Conf2 { @Autowired Foo foo; ... }


2 Answers

Inner test configuration

Example of an inner @Configuration for your test:

@RunWith(SpringRunner.class) @SpringBootTest public class SomeTest {      @Configuration     static class ContextConfiguration {         @Bean         @Primary //may omit this if this is the only SomeBean defined/visible         public SomeBean someBean () {             return new SomeBean();         }     }      @Autowired     private SomeBean someBean;      @Test     public void testMethod() {         // test     } } 

Reusable test configuration

If you wish to reuse the Test Configuration for multiple tests, you may define a standalone Configuration class with a Spring Profile @Profile("test"). Then, have your test class activate the profile with @ActiveProfiles("test"). See complete code:

@RunWith(SpringRunner.class) @SpringBootTests @ActiveProfiles("test") public class SomeTest {      @Autowired     private SomeBean someBean;      @Test     public void testMethod() {         // test     } }  @Configuration @Profile("test") public class TestConfiguration {     @Bean     @Primary //may omit this if this is the only SomeBean defined/visible     public SomeBean someBean() {         return new SomeBean();     } } 

@Primary

The @Primary annotation on the bean definition is to ensure that this one will have priority if more than one are found.

like image 67
alexbt Avatar answered Sep 22 '22 04:09

alexbt


You should use spring boot profiles:

  1. Annotate your test configuration with @Profile("test").
  2. Annotate your production configuration with @Profile("production").
  3. Set production profile in your properties file: spring.profiles.active=production.
  4. Set test profile in your test class with @Profile("test").

So when your application starts it will use "production" class and when test stars it will use "test" class.

If you use inner/nested @Configuration class it will be be used instead of a your application’s primary configuration.

like image 24
Slava Babin Avatar answered Sep 22 '22 04:09

Slava Babin