Skip to content

@MockitoBean is not supported in @Component class like @MockBean is #34415

@Sax388

Description

@Sax388

I expected @MockitoBean to also be injected into the Spring Context as @MockBean did.

Minimal reproducible project: https://github.com/Sax388/mockito-bean-failing-when-mock-bean-does-not

When using

@MockitoBean private TokenService tokenService;

in https://github.com/Sax388/mockito-bean-failing-when-mock-bean-does-not/blob/c75c41eecb69080ea66292c07930b419de03a253/src/test/java/com/example/demo/cucumber/steps/RequestHelper.java#L24
then the test fails with

Cannot invoke "com.example.demo.TokenService.getTenantName()" because "this.tokenService" is null
java.lang.NullPointerException: Cannot invoke "com.example.demo.TokenService.getTenantName()" because "this.tokenService" is null
	at com.example.demo.cucumber.steps.RequestHelper.adminRequest(RequestHelper.java:27)
	at com.example.demo.cucumber.steps.TestSteps.makeTestRequest(TestSteps.java:16)
	at ✽.I make a test request(classpath:features/test.feature:4)

When using

@MockBean private TokenService tokenService;

the test passes.

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: invalidAn issue that we don't feel is valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions