I have a @SpringBootTest
class that has a rather complex mock definition setup with mocked return values.
Question: can I externalize @MockBean
setups into an own class, so I could reuse the mock configuration in multiple class (sidenote: I'm not looking for inheritance here!).
@SpringBootTest
public class ServiceTest extends DefaultTest {
@Autowired
private ServiceController controller;
@MockBean
private Service1 s1;
@MockBean
private Service2 s2;
@MockBean
private Service3 s3;
//assume more complex mock definitions
@BeforeEach
public void mock() {
when(s1.invoke()).thenReturn(result1);
when(s2.invoke()).thenReturn(result2);
when(s3.invoke()).thenReturn(result3);
}
@Test
public void test() {
//...
}
}
I want to load the mocks independently of each other, not globally for all my tests.
@Import(MockService1.class)
on my tests? That's indeed a nice idea, I always struggled with this because it's impossible to inject@MockBean
in this case. But theMockito.mock()
seems to work! – Medical