I've a problem with the MockBean when running multiple test (reducing the success-rate from 100% to ~10%).
The setup is like this:
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class FirstIT{
@Autowired
private MockMvc mockMvc;
@Test
void testGetUser() throws Exception {
..
@SpringBootTest
@ActiveProfiles("test")
class SecondIT{
@MockBean
private UserService userService;
@Test
void testSomethingElse() throws Exception {
...
Mockito.verify(userService, Mockito.timeout(2000).atLeastOnce()).processUserAfterUpload(Mockito.any());
}
When running it stand-alone, it it successful. The debugger shows that the class is a Mockito mock on runtime.
Now when running together (with many more tests), it fails with Actually, there were zero interactions with this mock.. When I debug, I can see that userService is injected as the real object for the SecondIT~testSomethingElse.
I somehow think the context is reused, and MockBean completly ignored if it doesn't run first.
Do I have to annotate every single IT class with @DirtiesContext? I mean it's not possible to say reset context before and after this class.
But I'm also not sure that this is intended. That would mean MockBean never works correctly with multiple tests.