Mockito: Allow @InjectMock into static/final fields

Created on 30 Jun 2018  路  4Comments  路  Source: mockito/mockito

For some of my projects it would be nice if @InjectMock would work with spies on final/static fields.
Unfortunately I cannot refactor the code to use constructor/setter injection since this would alter the API (package-private is not sufficient).

I can see that injecting into static final is problematic (unexpected test behavior on subsequent tests)...
but injecting into just static or final fields should pose no problems - at least none I can see (care to enlighten if your opinion differs?).

injection

Most helpful comment

Actual use case:
my instance does have an internal cache of some sort:
private final Cache cache = new Cache();

I do not ever want to expose this cache in the API of the containing class (not event package-private) since this is a very private implementation detail no one else should know/care of.

In general, relying on static state during tests is bad, especially if mocks are mixed in.

Fully agree, but not being able to easily test the classes containing static state is (imho) event worse.
Workaround is to set these class-fields via reflection - which is bad, too.

PowerMock already handles this case

This requires yet another framework everyone on the team has to learn.

as it encourages bad mocking behavior

Not being able to mock easily encourages skipping testing altogether :(

All 4 comments

Since I could not find any workaround I tried to implement a possible solution in #1418 using a new annotation: @InjectUnsafe

If there might be other, better ways to accomplish this (custom AnnotationEngine?, other way of hooking into Mockito?): could you please give hints?

I am inclined to not support this feature, as it encourages bad mocking behavior. Moreover, I think PowerMock already handles this case. In general, relying on static state during tests is bad, especially if mocks are mixed in. Writing adapters in that case improves readability.

For some of my projects it would be nice if @InjectMock would work with spies on final/static fields.

Can you elaborate the use case? E.g. why "it would be nice". Thank you for reporting!

Actual use case:
my instance does have an internal cache of some sort:
private final Cache cache = new Cache();

I do not ever want to expose this cache in the API of the containing class (not event package-private) since this is a very private implementation detail no one else should know/care of.

In general, relying on static state during tests is bad, especially if mocks are mixed in.

Fully agree, but not being able to easily test the classes containing static state is (imho) event worse.
Workaround is to set these class-fields via reflection - which is bad, too.

PowerMock already handles this case

This requires yet another framework everyone on the team has to learn.

as it encourages bad mocking behavior

Not being able to mock easily encourages skipping testing altogether :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

d1231 picture d1231  路  5Comments

miaekim picture miaekim  路  5Comments

eulogi picture eulogi  路  4Comments

Raghu290 picture Raghu290  路  4Comments

konradkluz picture konradkluz  路  4Comments