Hi,
I have a test class annotated with @SpringBootTest that uses a static inner @TestConfigurationclass to override a bean in the application context:
@SpringBootTest
class DefaultHelloServiceTest {
@TestConfiguration
static class MyTestConfiguration {
@Bean
@Primary
HelloService helloService() {
var helloService = new HelloService("Spring");
return helloService;
}
}
// ...
}
In Spring Boot 3.5.x MyTestConfiguration is evaluated even for tests in my test class, that are @Nested JUnit test classes:
@SpringBootTest
class DefaultHelloServiceTest {
// ... as above
@Nested
class TestBean {
@Test
void test_bean_nested() {
// ✅ WORKS in Spring Boot 3.5.x
// helloService is from MyTestConfiguration
assertEquals("Spring", helloService.getGreeting());
}
}
}
In Spring Boot 4 this does not work anymore. The MyTestConfiguration.helloService-Method is only called when a test method is executed, that is not inside a @Nested test class. Instead of my customized service the "normal" one from the application context is injected.
When adding an explicit @Import(DefaultHelloServiceTest.MyTestConfiguration.class) to the test class, it also works in both Spring Boot versions.
I have created a repository with two simple projects (3.5.7 and 4.0.0) that reproduces the behavior.
The two test classes:
Not sure what the correct behavior is and if I'm doing something wrong here, but for me it seems, that the behavior has changed.
Hi,
I have a test class annotated with
@SpringBootTestthat uses a static inner@TestConfigurationclass to override a bean in the application context:In Spring Boot 3.5.x
MyTestConfigurationis evaluated even for tests in my test class, that are@NestedJUnit test classes:In Spring Boot 4 this does not work anymore. The
MyTestConfiguration.helloService-Method is only called when a test method is executed, that is not inside a@Nestedtest class. Instead of my customized service the "normal" one from the application context is injected.When adding an explicit
@Import(DefaultHelloServiceTest.MyTestConfiguration.class)to the test class, it also works in both Spring Boot versions.I have created a repository with two simple projects (3.5.7 and 4.0.0) that reproduces the behavior.
The two test classes:
Not sure what the correct behavior is and if I'm doing something wrong here, but for me it seems, that the behavior has changed.