Skip to content

Test Bean Overrides honor fallback qualifier instead of @Primary semantics #34374

Closed
@patdlanger

Description

@patdlanger

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

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions