-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test Bean Overrides honor fallback qualifier instead of @Primary
semantics
#34374
Comments
@MockitoBean
behaviour with subtyped class annotated @Primary
@MockitoBean
behaviour with subclassed class annotated @Primary
Hi @patdlanger, Congratulations on submitting your first issue for the Spring Framework! 👍 I tried to reproduce the behavior you're reporting with the following. @SpringJUnitConfig
public class MockPrimaryBeanTests {
@Autowired
ServiceA serviceA;
@MockitoBean
BaseService baseService;
@Test
void contextLoads() {
serviceA.callB();
verify(baseService).doSomething();
}
@Configuration
@Import({ BaseService.class, ServiceA.class, ServiceB.class })
static class Config {
}
@Component
public static class BaseService {
public void doSomething() {
}
}
@Primary
@Component
public static class ServiceB extends BaseService {
}
@Component
public static class ServiceA {
private final BaseService serviceB;
public ServiceA(BaseService serviceB) {
this.serviceB = serviceB;
}
public void callB() {
this.serviceB.doSomething();
}
}
} But that passes for me with With which version of |
Related Issues |
Hi @sbrannen , |
Thanks for the feedback and the reproducer, @patdlanger. 👍
On the surface, yes, that appears to be the case. However, after debugging it, I figured out that the core difference results from the use of With component scanning the bean names are With The latter is what causes this bug. Specifically, when the name of the In the following modified version of my example, each The test class fails "as is", but if you change the name of the field to something other than @SpringJUnitConfig
class MockPrimaryBeanTests {
@Autowired
ServiceA serviceA;
@MockitoBean
BaseService baseService;
@Test
void contextLoads() {
serviceA.callB();
verify(baseService).doSomething();
}
@Configuration
@Import({ BaseService.class, ServiceA.class, ServiceB.class })
static class Config {
}
@Component("baseService")
static class BaseService {
public void doSomething() {
}
}
@Primary
@Component("serviceB")
static class ServiceB extends BaseService {
}
@Component("serviceA")
static class ServiceA {
private final BaseService serviceB;
public ServiceA(BaseService serviceB) {
this.serviceB = serviceB;
}
public void callB() {
this.serviceB.doSomething();
}
}
} |
@MockitoBean
behaviour with subclassed class annotated @Primary
@Primary
semantics
I just pushed a fix for this which will be available in the upcoming 6.2.3 release. However, @patdlanger, it would be great if you could try out a 6.2.3 snapshot before the release to confirm that everything is good on your end. Cheers! |
Thanks @sbrannen the snapshot fixes our issue. |
Great! Thanks for letting us know. |
Using Spring Boot 3.4.2
Bug Reports
Given the following classes
The following test works with @MockBean but fails with @MockitoBean because the mocked bean is not injected into
ServiceA
Enhancements requests
This used to/does work with @MockBean
The text was updated successfully, but these errors were encountered: