Using Spring Boot 3.4.2
Bug Reports Given the following classes
@Component
public class BaseService {
public void doSomething() {}
}
@Primary
@Component
public class ServiceB extends BaseService {
}
@Component
public class ServiceA {
private final BaseService serviceB;
public ServiceA(BaseService serviceB) {
this.serviceB = serviceB;
}
public void callB() {
serviceB.doSomething();
}
}
The following test works with @MockBean but fails with @MockitoBean because the mocked bean is not injected into ServiceA
@SpringBootTest
class MockitobeanApplicationTests {
@Autowired
ServiceA serviceA;
@MockitoBean
BaseService baseService;
@Test
void contextLoads() {
serviceA.callB();
verify(baseService).doSomething();
}
}
Enhancements requests This used to/does work with @MockBean
Comment From: sbrannen
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 @MockBean
and @MockitoBean
(against the 6.2.x branch).
With which version of spring-test
did you encounter the failure?
Comment From: sbrannen
Related Issues
-
33819