I don't know much findbugs, since I use sonar. For static code analysis tools I usually find default rules not always the best so I tend to tweak them and update them for the new development pattern.
Anyway for findbugs, googling around gave two possible solution
Use findbug annotation on each field (findbugs 3.0.0)
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value="UR",
justification="This is an auto injected mock")
Or set a filter to ignore the Mock
annotation.
<Match>
<Bug code="UR">
<Field annotation="Mock">
</Match>
Note that both of these is pseudo code, I'm writing from a smartphone. So there may be missing mandatory statements and/or the rule (UR
) may be wrong, the bug description is on the findbug website.
These ideas mostly came from :
- Is there a way to ignore a single FindBugs warning?
- http://www.scriptscoop2.com/t/0044ed425b4f/java-how-to-set-a-findbugs-filter-for-fields-with-a-specific-annotation.html
- http://findbugs.sourceforge.net/bugDescriptions.html
I'd like to point out that the way mockito is used in this example does seem wrong. Here's my recommendation :
User
is either an entity or a value object I strongly advise to not mock such classes. For these I prefer to use builders. Usually I craft a test builder that can be used like that UserBuilder.userWithId(73L).build()
- And finally instead mock
UserService
to return the preconfigured user BDDMockito.given(user_service_mock.getUser()).willReturn(preconfigured_user)