Spring Boot Test Starter to make it possible again to use @MockitoBean
in @Configuration
for tests.
Since Spring Boot 3.4 @MockBean
and @MockitoSpyBean
became deprecated because support for mocking and spying beans in tests is now directly supported by Spring Framework. The new annotations for this are @MockitoBean
and @MocktioSpyBean
and you can now also implement your own annotation that overrides beans in a test context.
Old Annotation | New Annotation |
---|---|
@MockBean |
@MockitoBean |
@SpyBean |
@MockitoSpyBean |
Sadly, not all features are from Boot are supported by the new annotations. Namely, the following does not work with the new annotations:
Works (old Spring Boot @MockBean
):
@SpringJUnitConfig
class MyCfgTests() {
@Autowired
MyThing myThingMock;
@Test
void test() {
}
@Configuration
public static class MyTestConfig {
@MockBean //This works
MyThing myThingMock;
}
}
Does not work out of the box (new @MockitoBean
):
@SpringJUnitConfig
class MyCfgTests() {
@Autowired
MyThing myThingMock;
@Test
void test() {
}
@Configuration
public static class MyTestConfig {
@MockitoBean //This does not
MyThing myThingMock;
}
}
A list of things not supported anymore:
- Discovery of these annotations inside
@Configuration
beans: spring-projects/spring-framework#33934 - Directly mocking a
BeanFactory
: spring-projects/spring-framework#34653 - Creating the bean spied on alongside the spy: spring-projects/spring-framework#33935
But since there are scenarios where it is not practical to create a common annotation for sharing mocks/spies between tests, this project
brings back the support for discovery of these annotations on @Configuration
beans.
Just import via
<dependency>
<groupId>io.github.duoduobingbing</groupId>
<artifactId>beanoverride-in-config-starter-test</artifactId>
<version>[1.0,)</version>
<scope>test</scope>
</dependency>
inside your pom.xml
.
The dependency defines a new ContextCustomizerFactory
that is autodiscovered via this dependency's spring.factories
file.
You can now enjoy working discovery inside your @Configuration
classes:
@SpringJUnitConfig
class MyCfgTests() {
@Autowired
MyThing myThingMock;
@Test
void test() {
}
@Configuration
public static class MyTestConfig {
@MockitoBean
MyThing myThingMock;
}
}
or in another class like this:
@TestConfiguration
class SomeConfigWithMockitoBean() {
@MockitoBean
MyThing myThingMock;
}
@SpringJUnitConfig
@Import(SomeConfigWithMockitoBean.class)
class MyCfgTests() {
@Autowired
MyThing myThingMock;
@Test
void test() {
}
}
Your own custom implementations with e.g. @BeanOverride(YourCustomBeanOverrideProcessor.class)
will be discovered as well.
Minimum Java Version: 21 (LTS)
beanoverride-in-config Version | Supported Spring Boot Version(s) |
---|---|
1.0 | 3.4.5-3.4.X1 3.5.X |
1.1 (not released yet) | 4.0.0 |
Footnotes
-
❌️ Not supported: Spring Boot ≦ 3.4.4 ↩