问题
I'm following the Vogella tutorial on Mockito and get stuck pretty much immediately. 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....
}
}
回答1:
The method when() is not part of your class MyClass. It's part of the class Mockito:
Mockito.when(test.getUniqueId()).thenReturn(43);
or, with a static import:
import static org.mockito.Mockito.*;
...
when(test.getUniqueId()).thenReturn(43);
回答2:
you are trying to get when from the mocked class. You need to do something like:
...
MyClass test = Mockito.mock(MyClass.class);
Mockito.when(test.getUniqueId()).thenReturn(43);
...
来源:https://stackoverflow.com/questions/25268783/cannot-resolve-method-when