0

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()

iton
  • 1
  • 3
  • 3
    Error Message?? – Yati Sawhney May 10 '18 at 15:18
  • 1
    Off topic: Your setup method looks like you might want to read up on `@BeforeClass`. – Marvin May 10 '18 at 15:19
  • works for me, please provide us with exception being thrown – Maciej Dobrowolski May 10 '18 at 15:34
  • You shouldn't initialize MockitoAnnotations only once, in that way you risk that multiple tests are dependent on each other (or mocks are not initialized correctly). What happens if you remove `mockInitialized` and just call `MockitoAnnotations.initMocks(this);` in your `setUp` method? – user2456718 May 10 '18 at 16:16
  • for @BeforeClass it needs to be static, that is supposed to be a workaround for that. If I delete The mockInitialized it doesnt change anything. I added the Errormessage to the Post. – iton May 11 '18 at 02:06
  • try what's proposed in https://stackoverflow.com/questions/15904584/mockito-gives-unfinishedverificationexception-when-it-seems-ok, at least it should give you the place where the error really is – user2456718 May 11 '18 at 07:12

0 Answers0