Mockito methods not available

I have a mockito setup in my project using these maven lines:

<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.8.5</version> <scope>test</scope> </dependency> 

I have no problem using the @Mock annotation, but I cannot access and use the mockito methods, for example:

 when(someMock.someMethod()).thenReturn(); 

Eclipse just does not recognize them.

Please, help.

+7
source share
1 answer

Try calling Mockito.when(foo.getBar()).thenReturn(baz) and Mockito.verify(foo).getBar() , which will not rely on static imports. Unlike the @Mock annotation, which is technically a class, when and verify static methods in the Mockito class.

Once you get started, try the static import referenced by David:

 import static org.mockito.Mockito.when; // ...or... import static org.mockito.Mockito.*; // ...with the caveat noted below. 

This will allow you to use Mockito.when without specifying the Mockito class. You can also use a wildcard, as it is, but answer to this SO that Java docs recommend using wildcards sparingly - moreover, it can break if a static method with a similar name is added to Mockito.

Adding import org.mockito.*; not enough because it adds all the classes to the org.mockito package, but not the methods on org.mockito.Mockito .

For Eclipse in particular, you can add static imports by placing the cursor on when it is part of Mockito.when and pressing Control-Shift-M ("Add Import"). You can also add org.mockito.Mockito to Favorites (Window> Preferences> Java> Editor> Content Assist> Favorites> New Type) so that all static Mockito methods appear in the tooltip for the contextual content of Ctrl-Space, even if you have " I didn’t import them specially. (You can also do this for org.mockito.Matchers, which are technically available on org.mockito.Mockito through inheritance, but may not appear in Eclipse for this reason.)

+25
source

All Articles