I want to test the http layer for an spring boot rest application. For this mocked the service to answer the calls of the controller.
Since i have many methods (eg findBy...) that expect a list to be returned by the service i want to stub all calls at once and all should be answered by the same answer.
For one it is:
when(someService.getAll()).thenReturn(listOfSomeElements);
Is there a way in Mockito to setup same answer for multiple calls that all accept the same answer? Something like
when(someService.getAll(), someService.getSome(), someService.getFew()).thenReturn(listOfSomeElements);
or
doReturn(listOfSomeElements).when(someService.getAll()).when( someService.getSome()).when(someService.getFew())
You can store method references and then iterate over them
interface Foo {
String foo();
}
interface Bar {
String bar();
}
@Mock
Foo mockFoo;
@Mock
Bar mockBar;
@Test
public void someTest()
{
List<Supplier<String>> suppliers = Arrays.asList(mockFoo::foo, mockBar::bar);
for (Supplier<String> supplier : suppliers)
{
when(supplier.get()).thenReturn("Blah");
}
assertEquals("Blah", mockFoo.foo());
assertEquals("Blah", mockBar.bar());
}
This test is runnable and passes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.