Mockito methods are not accessible

Kotlin syntax - dont forget the ` ` backticks:

import org.mockito.Mockito.`when`


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

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

Once you have that working, then try the static imports to which David alluded:

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

This will then allow you to use Mockito.when without specifying the Mockito class. You can also use a wildcard, as so, but per this SO answer the Java docs recommend using wildcards sparingly--especially since it can break if a similarly-named static method is ever added to Mockito later.

Adding import org.mockito.*; is insufficient because that adds all classes in the org.mockito package, but not the methods on org.mockito.Mockito.

For Eclipse in particular, you can add a static import by putting the cursor on the when part of Mockito.when and pressing Control-Shift-M ("Add import"). You can also add org.mockito.Mockito to your Favorites (Window > Preferences > Java > Editor > Content Assist > Favorites > New Type) so that all Mockito static methods show up in your Ctrl-Space content assist prompt even if you haven't imported them specifically. (You may also want to do this for org.mockito.Matchers, which are technically available on org.mockito.Mockito via inheritance, but may not show up in Eclipse for that reason.)

Tags:

Mockito