@RunWith(MockitoJUnitRunner.class)
public class TestMyStuff{
@Mock
private Worker worker;
@Before
public void setup(){
Mockito.lenient()
.when(worker.doWork("some stuff", "some other stuff", new Date()))
.thenReturn(true);
Mockito.lenient()
.doReturn(true)
.when(worker).doWork("some stuff", "some other stuff", new Date());
}
@Test
public void test(){
//quick test to see if mock works.
final boolean isDone = worker.doWork("zzz", "qwerty);
System.out.println("isDone: " + isDone);//i want isDone to be true
}
}
Mockito.lenient()
方法似乎不起作用,因为它没有返回true
。如何使该方法Worker.#doWork()
无论输入如何都返回true
?
你可以使用这个: when(Worker.doWork(anyString(),...).thenReturn(true)
但是,我认为您发布的代码存在一些问题。"doWork"方法似乎需要两个字符串和一个日期对象。在测试方法中,您应该为"doWork"提供适当的输入以查看结果。