cannot resolve method “when”

只谈情不闲聊 提交于 2021-01-18 11:10:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!