I have a class for which I am writing a JUnit test. I am trying to test if a particular method is never called.

public class CountryProcess extends AbstractCountryProcess {

    private static final Logger log = LoggerFactory.getLogger(CountryProcessor.class);
    private static final Long MAX_FILE = 20l;

    @Override
    protected boolean processCountry(Region region, City city) {
        Long maxFile = region.getRequiredLongValue(SIZE);

        if (maxFile < MAX_FILE) {
            cntDao.addCountryLandMark(city);
        }
        else {
            log.warn("File size was big");
        }

        return true;
}

And the test class is:

public class CountryProcessTest {

    @Rule
    public final JUnitRuleMockery context = new JUnitRuleMockery();
    private final CntDao cntDao = context.mock(CntDao.class);

    @Before
    public void setup() {
        Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                bind(cntDao.class).toInstance(cntDao);
            }
        });
    }

    @Test
    public void shouldIgnoreIfFileSizeBiggerThanPermitted() {
        //some code to make it trigger ELSE statement above...
        verify(cntDao, never()).addCountryLandMark(anyString());
    }
}

But this returns the following error:

org.mockito.exceptions.misusing.NotAMockException:

Argument passed to verify() is of type $Proxy4 and is not a mock!

Make sure you place the parenthesis correctly!

See the examples of correct verifications:

verify(mock).someMethod();

verify(mock, times(10)).someMethod();

verify(mock, atLeastOnce()).someMethod();

Any idea how I can fix this in the current context. Please give an example using current code so I get a better idea?

1 Answers

2
Lesiak On

You are mixing two mocking frameworks:

  • jMock - JUnitRuleMockery
  • Mockito - verify method

Clearly, they are not compatible with each other. Your verify call looks ok, I believe it will work as soon as it receives a mock created with Mockito (Use Mockito.mock(CntDao.class))

As an alternative to never you can use Mockito.verifyNoMoreInteractions or Mockito.verifyZeroInteractions, but they are less specific.