@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
?
答案 0 :(得分:1)
您可以使用此:
when(Worker.doWork(anyString(),...).thenReturn(true)
但是,我认为您发布的代码有一些问题。 “ doWork”方法似乎采用两个String和一个Date对象。在测试方法中,您应该向“ doWork”输入适当的信息以查看结果。