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....
}
}
As before cannot resolve symbol means the java compiler cannot find the symbol it shows you on the symbol line. In this case it cannot find a method called Println for the object out . This is true, there is no such method, what we meant was println with a lowercase p.
Before we can use Mockito for mocking final classes and methods, we have to configure it. Mockito checks the extensions directory for configuration files when it is loaded. This file enables the mocking of final methods and classes.
You can use doReturn-when to specify a return value on a spied object without making a side effect. It is useful but should be used rarely. The more you have a better pattern such as MVP, MVVM and dependency injection, the less chance you need to use Mockito. spy .
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With