mantamusica
mantamusica

Reputation: 220

mockito, wanted but not invoked

I have a problem. I create object with mockito. Then I do the verification of the method and when running the test, it gives me error of Wanted but not invoked. And that the service stays as ().

@Test
    public void recordTest() throws IOException, URISyntaxException
    {

    URL resourceUrl = getClass().getResource(F1);
    Path resourcePath = Paths.get(resourceUrl.toURI());

    Object object = new Object ();
    when(objectServiceMock.getObjectByNem((Nem) anyObject())).thenReturn(object);

    Page<HorvarATPF> pageHorvar = new Page<HorvarATPF>();
    when(horvarATPFServiceMock.getHorvarATPFs((FilterHorvarATPF) anyObject())).thenReturn(pageHorvar);

    horvarATUtilService.record(resourcePath.toFile());

    verify(objectServiceMock, times(1596)).getObjectByNem((Nem) anyObject());

}

test doesn´t run in line of verify, with Wanted buy not invoked.

Upvotes: 2

Views: 6118

Answers (1)

xyz
xyz

Reputation: 5407

The proble is

verify(objectServiceMock, times(1596)).getObjectByNem((Nem) anyObject())

Mockito expects that you call this method 1596 times.

But you declare that it calls just once.

when(objectServiceMock.getObjectByNem((Nem) anyObject()).

To fix test just put

verify(objectServiceMock).getObjectByNem((Nem) anyObject());

or

verify(objectServiceMock, times(1)).getObjectByNem((Nem) anyObject())

here is examples from mockito documentation Verifying exact number of invocations / at least x / never

Upvotes: 3

Related Questions