6

If you declare a private field in a test class:

private Foo foo;

That gets used but is never assigned, IntelliJ IDEA (and presumably other IDEs) show a warning when I hover over the declaration, and it renders it gray

private field 'foo' is never assigned

But if I'm using Mockito and annotate the field like:

@Mock private Foo foo;

The warning disappears and renders the field in purple to indicate that it is assigned. How does the IDE know what @Mock implies? I've had a look through the Mockito source code and there doesn't seem to be anything on the annotation definition and my IDE doesn't have a Mockito plugin.

I'm writing a library similar to Mockito with an equivilant annotation and I'd love to know to get the IDE to remove the warning.

(I'm not a Java developer, so if I've said anything wrong please correct me)

EDIT I've tried this in Eclipse and it actually removes the warning with any annotation, which is what @nikpon was suggesting.

RichK
  • 11,318
  • 6
  • 35
  • 49
  • `private` fields are always assigned to a default value. I've seen the "foo is never assigned" in reference to _local variables_, but never _fields_. – Charles Forsythe Nov 01 '13 at 18:26
  • In IntelliJ I'm definitely getting the warning on a field. The color of the unassigned field is gray, but turns purple when @Mock is applied to it (and the warning message goes away). – RichK Nov 01 '13 at 18:33
  • That's just weird. Maybe IntelliJ maintains a list of "auto-populate" annotations so that it can modify this warning behavior. Technically, there is no such thing as an unassigned field, so I would never expect to see this warning ever. If I saw it on `@Mock` or `@Autowired` fields it would drive me nuts. Perhaps they "fixed" that for such cases. – Charles Forsythe Nov 01 '13 at 18:40
  • Yes my assumption was that Mockito is so ubiquitous that it just special cases @Mock which would be a shame for me. – RichK Nov 01 '13 at 18:42

1 Answers1

7

The "unused symbol" inspection in IntelliJ IDEA has special configuration for this annotation.

A quick search for "org.mockito.Mock" in the intellij-community code base reveals the JUnitEntryPoint class, which plugs into the "deadCode" extension point.

The entry points contributed through this mechanism are in turn queried by inspections like UnusedDeclarationInspection.

Note that the inspection has a configuration pane where you can configure additional custom annotations that should prevent a field from being marked as unused.

You can access this configuration from Settings->Inspections, then search for the "unused symbol" inspection by name: screenshot of the "unused symbol" inspection configuration

For convenience, you can also navigate to these settings directly, by invoking Alt-Enter while the caret is on a field marked as unused by IDEA: screenshot of accessing the "unused symbol" settings by invoking Alt-Enter on an unused field

Pakka Pakka
  • 2,046
  • 15
  • 9
  • 4
    Hmm, isn't this only half a solution to the problem? I'd like to have IntelliJ warn me when I declare a @Mock member variable, but never use it, which is not possible... – Rens Verhage Nov 27 '13 at 08:33
  • 2
    Good point. I've filed [IDEA-118095](http://youtrack.jetbrains.com/issue/IDEA-118095) to improve this. Please take a look and add your comments. – Pakka Pakka Dec 11 '13 at 21:08