@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”方法看起来像是采用两个 String 和一个 Date 对象。在测试方法中,您应该向“doWork”提供正确的输入以查看结果。
关于java - Mockito.lenient 未返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359645/