99

I have a Bean defined in a class decorated with @Configuration:

@Configuration
public class MyBeanConfig {
    @Bean
    public String configPath() {
        return "../production/environment/path";
    }
}

I have a class decorated with @TestConfiguration that should override this Bean:

@TestConfiguration
public class MyTestConfiguration {
    @Bean
    @Primary
    public String configPath() {
        return "/test/environment/path";
    }
}

The configPath bean is used to set the path to an external file containing a registration code that must be read during startup. It is used in an @Component class:

@Component
public class MyParsingComponent {
    private String CONFIG_PATH;
    
    @Autowired
    public void setCONFIG_PATH(String configPath) {
        this.CONFIG_PATH = configPath;
    }
}

While trying to debug this I set a breakpoint inside each method as well as the constructor of the test config class. The @TestConfiguration's constructor breakpoint is hit, so i know that my test configuration class instantiates, however the configPath method of that class is never hit. Instead, the configPath method of the normal @Configuration class is hit and the @Autowired String in MyParsingComponent is always ../production/environment/path rather than the expected /test/environment/path.

Not sure why this is happening. Any thoughts would be greatly appreciated.

7
  • 4
    Is your test class annotated with @Import(MyTestConfiguration.class)? Commented May 31, 2018 at 14:17
  • 3
    @SamBrannen It wasn't there. And adding it didn't work. But changing it to @ContextConfiguration(MyTestConfiguration.class) did. Still don't understand why the @Primary annotation was being ignored without the ContextConfiguration, though. Commented May 31, 2018 at 15:17
  • 1
    As documented in the Spring Boot reference manual, any beans configured in a top-level class annotated with @TestConfiguration will not be picked up via component scanning. So that's why you have to explicitly declare it. Commented May 31, 2018 at 16:23
  • If you class annotated with @TestConfiguration were a static nested class within your test class, it would be used automatically. Commented May 31, 2018 at 16:24
  • If that answers your question(s), I can make it a formal answer. ;-) Commented May 31, 2018 at 16:24

9 Answers 9

131

As documented in the Detecting Test Configuration section of the Spring Boot reference manual, any beans configured in a top-level class annotated with @TestConfiguration will not be picked up via component scanning. So you have to explicitly register your @TestConfiguration class.

You can do that either via @Import(MyTestConfiguration.class) or @ContextConfiguration(classes = MyTestConfiguration.class) on your test class.

On the other hand, if your class annotated with @TestConfiguration were a static nested class within your test class, it would be registered automatically.

12
  • 1
    I try to do a test with #TestConfiguration placed on a top-level class and with #Import(MyTestConfiguration.class) and without #TestConfiguration but with #Import(MyTestConfiguration.class). The result was equal. Does it mean that #TestConfiguration is useless?
    – Valeriy K.
    Commented Feb 14, 2019 at 9:21
  • @TestConfiguration is still useful. Please consult the linked documentation for details. Commented Feb 15, 2019 at 10:04
  • I would like to reuse @TestConfiguration for multiple test classes hence having an abstract test class all the others do inherit might be an option. Unfortunately, @TestConfiguration is not picked up if it is in the abstract class the current test inherits from. @SamBrannen is it by design? How to share @TestConfiguration in-between test classes?
    – rilaby
    Commented Nov 29, 2020 at 13:59
  • Why I can not override a spring bean (with same name) with @Import annotation .. I can override with @ContextConfiguration and static inner class annotated as @TestConfiguration Commented Nov 19, 2021 at 12:42
  • Just a heads up, try both @Import(Your.class) and @ContextConfiguration(classes = Your.class) one after the other. Not sure why, but the former didn't work out for me. However, the latter did! Commented Mar 16, 2022 at 14:24
61

Make sure that the method name of your @Bean factory method does not match any existing bean name. I had issues with method names like config() or (in my case) prometheusConfig() which collided with existing bean names. Spring skips those factory methods silently and simply does not call them / does not instantiate the beans.

If you want to override a bean definition in your test, use the bean name explicitly as string parameter in your @Bean("beanName") annotation.

5
  • 10
    this is the actual answer. I have a nested static class with @TestConfiguration and it wasn't working. The problem was that the factory method was named the same as the actual bean.
    – Sebastian
    Commented Apr 19, 2020 at 21:21
  • 1
    In my very peculiar case I had to add @Primary as well. Otherwise it would just throw expected single matching bean but found 2.
    – Ivar
    Commented Apr 15, 2021 at 16:17
  • Marcel, this has been your first and only answer on SO - but it's a life saver! Thank you so much <3 Commented Jun 23, 2022 at 13:24
  • What is wrong here in my MRE github.com/gredwhite/springboottestconfigissue_demo/tree/bug/… ? Commented Sep 29, 2023 at 16:21
  • This helped me as well. I had same factory method names in both main and test conf and was struggling as nothing has been overriden. Adding @Primary also didn`t help. Thanks! Commented Aug 2 at 11:12
15

For me worked this code:

  @TestConfiguration // 1. necessary
  public class TestMessagesConfig {

    @Bean
    @Primary // 2. necessary
    public MessageSource testMessageSource() { // 3. different method name than in production code e.g. add test prefix

    }
  }
2
14
  • Test configuration has to be explicitly imported in the test via @Import({MyTestConfiguration.class}).
  • The name of the @Bean methods in @Configuration and @TestConfiguration have to be different. At least it makes difference in Spring Boot v2.2.
  • Also make sure spring.main.allow-bean-definition-overriding=true otherwise the bean could not be overriden.
2
7

I struggled with a related problem, whereby even though I was using an inner static class, my test bean was not being registered.

It turns out, You still need to add your inner static class to the @ContextConfiguration class array, otherwise the beans inside the @TestConfiguration doesn't get picked up.

public interface Foo {
    String execute();
}
public class FooService {
    private final Foo foo;

    FooService(Foo foo) {
        this.foo = foo;
    }

    public String execute() {
        return foo.execute();
    }
}
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {FooService.class, FooTest.FooTestConfig.class})
public class FooTest {
    @Autowired
    FooService fooService;

    @Test
    void test() {
        Assertions.assertEquals("MY_TEST_BEAN", fooService.execute());
    }

    @TestConfiguration
    static class FooTestConfig {
        @Bean
        public Foo getFooBean() {
            return () -> "MY_TEST_BEAN";
        }
    }
}
2
  • 1
    this is the only thing that helped, thank you for your answer.
    – Eugene
    Commented Sep 12, 2022 at 16:48
  • In case of @TestConfiguration being a static inner class in your test class, no need to explicitly register the configuration class, it will be picked up automatically. Commented Oct 27, 2022 at 13:35
6

In my case replacing @Import(TestConfig.class) with @ContextConfiguration(classes=TestConfig.class) did the trick. For some reason, some of the beans from TestConfig but 1 wasn't until I replaced @Import with @ContextConfiguration. This was also mentioned in some comments that were hidden because they had no upvotes.

1
5

I found it odd how several answers stated that the names of the @Beans have to be different from each other. How would that make one override the other?
There wasn't one specific answer that worked for me, but I've solved the issue by combining some of their advices.

Here's what worked for me.

Main configuration class:

@Configuration
public class SpringConfiguration {

    @Bean
    BeanInterface myBean() {
        return new BeanImplementation();
    }
    
    @Bean
    OtherClass otherBean() {
        return new OtherClass();
    }
}

Test configuration class:

@TestConfiguration
public class TestSpringConfiguration {

    @Bean
    @Primary
    BeanInterface myBean() {
        return new TestBeanImplementation();
    }
}

Test class:

@SpringBootTest(classes = TestSpringConfiguration.class, 
        properties = "spring.main.allow-bean-definition-overriding=true")
public class Tests {
    
    @Test
    public void test() {
        // do stuff
    }
}

In this way, the "myBean" bean instance is the one defined in the TestSpringConfiguration class, while "otherBean" is the one defined in the SpringConfiguration class, since it's not overridden.
If I gave two different names to the "myBean" beans, the "real" one would still be initialized and, in my case, would give an error during tests, since it needs something that's only available at runtime in its proper environment.
Once I gave both the same name, Spring would throw an error saying that they were conflicting. Hence why I had to specify the property spring.main.allow-bean-definition-overriding=true in the @SpringBootTest annotation of the test class.

P.S. As noted by @PavelD. in the comments, if you have to specify multiple test configuration classes in the @SpringBootTest annotation the test ones with beans that need to override the originals have to be specified first, in the list.



By the way, if you're NOT using Spring Boot, I guess these alternative annotations could work for you:

@ExtendWith(value = SpringExtension.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, // <- not sure about this one
        classes = { SpringConfiguration.class, TestSpringConfiguration.class })
public class Tests {
    
    @Test
    public void test() {
        // do stuff
    }
}

Then, you would still have to set the property spring.main.allow-bean-definition-overriding=true in the test application.yml or application.properties file, or in some other way via code on startup.

Note: I'm not 100% sure that you would need the loader = AnnotationConfigContextLoader.class thing. Try without it, first. I needed it in a project of mine which had Spring without Boot, but I can't remember whether it's a standard thing to set or I needed it for some specific reason.

4
  • This worked great for my case when I needed to override the production bean in a way it won't be loaded at all in tests, fully replaced by the bean configured in tests.
    – Pavel D.
    Commented Apr 16 at 13:42
  • 1
    Adding to this answer, it is very important that the @TestConfiguration class is listed in @SpringBootTest(classes = ...) and if this list contains also production configuration classes, the test configuration class must come first. Otherwise, @TestConfiguration is loaded, but only after production configuration, unable to override production beans already defined with same name.
    – Pavel D.
    Commented Apr 16 at 13:50
  • Thank you @PavelD. I didn't know about that detail. Should I add it to the answer?
    – nonzaprej
    Commented Apr 22 at 16:16
  • Your answer alredy contains @SpringBootTest(classes = TestSpringConfiguration.class. You can add my note to your answer if you wish. I just wanted to emphasize that if there are multiple configuration classes there, (not common scenario, I guess), the test one must come first. In my specific project there were other classes there so I had to do some debugging and trial and error until I figured it out.
    – Pavel D.
    Commented Apr 22 at 20:20
2

I came across a similar issue recently and got it sorted out by annotating my testing bean with @Primary as well as @Bean. Not sure why it's required, which seems not documented in the Spring doc. The version of my SpringBoot is 2.0.3.

1
  • I was searching for hours for this answer! Thank you, it worked
    – MrNobody
    Commented Feb 4, 2022 at 12:11
0

In my case it was an issue with @RunWith(SpringRunner.class), I'm not exactly sure why it wasn't working, I was following this - Testing in Spring Boot

But after replacing that with @ExtendWith(SpringExtension.class) the inner static @TestConfiguration class created the beans as expected.

Maybe a version mismatch - I'm using Spring Boot 2.7.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.