I have some Unittests for my Project and they work if i run them with IntelliJ. However if i run them with Maven alot of them fail. I am using SpringBoot.
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = MainApplication.class)
public class ServiceTest {
private static boolean mockInitialized = false;
@Autowired
private IService Service;
@MockBean
private IDAO DAO;
@MockBean
private ISearchDAO SearchDAO;
@Before
public void setUp() {
if (!mockInitialized) {
MockitoAnnotations.initMocks(this);
mockInitialized = true;
}
}
@Test
public void getByIdPersists() throws Exception {
DTO dto = new DTO(1, "test");
when(DAO.getById(1)).thenReturn(dto);
DTO found = Service.getById(1);
verify(DAO).getById(1);
assertEquals(dto, found);
}
The executed DAO is not the same es the one to verify in Maven while it is in IntelliJ The Errormessage for this:
[ERROR] getByIdPersists(ServiceTest) Time elapsed: 0 s <<< ERROR! org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here: -> at ServiceTest.getByIdPersists(ServiceTest.java:130)
Example of correct verification: verify(mock).doSomething()