Cannot allow the when method

I follow the Vogella Mockito tutorial and get stuck right away. IntelliJ displays cannot resolve method 'when'for the class below.

... what did I miss?

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MockitoTest  {

@Test
public void test1()  {
    MyClass test = Mockito.mock(MyClass.class);
    // define return value for method getUniqueId()
    test.when(test.getUniqueId()).thenReturn(43);

    // TODO use mock in test.... 
}

}
+4
source share
2 answers

The method when () is not part of your MyClass class. This is part of the Mockito class:

Mockito.when(test.getUniqueId()).thenReturn(43);

or with static import:

import static org.mockito.Mockito.*;

...

when(test.getUniqueId()).thenReturn(43);
+12
source

which you try to get when from the mocking class. You need to do something like:

...
MyClass test = Mockito.mock(MyClass.class);
Mockito.when(test.getUniqueId()).thenReturn(43);
...
0
source

All Articles