简体   繁体   中英

How to check that a method is not being called using JUnit Mockito Verify

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?

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.

In addition to the answer from @Lesiak, here is a reproducible example based on your code with both conditions tested and BDD implementation as well (commented out).

@ExtendWith(MockitoExtension.class)
class CountryProcessTest {

    @Mock CountryDAO cntDao;

    @Mock
    Region region;

    @Mock
    City city;

    @InjectMocks
    CountryProcess countryProcess;


    @Test
    void processCountryLargeSize() {
        // given
        given(region.getRequiredLongValue()).willReturn(100L);

        // when
        countryProcess.processCountry(region, city);

        // then
        verifyNoInteractions(cntDao);
        // then(cntDao).shouldHaveNoInteractions(); // BDD implementation
    }

    @Test
    void processCountrySmallSize() {
        // given
        given(region.getRequiredLongValue()).willReturn(10L);

        // when
        countryProcess.processCountry(region, city);

        // then
        verify(cntDao).addCountryLandMark(city);
        verifyNoMoreInteractions(cntDao);
        // then(cntDao).should().addCountryLandMark(any()); // BDD implementation
        // then(cntDao).shouldHaveNoMoreInteractions(); // BDD implementation


    }
}

The rest of the classes here are provided for reference.

Region

public class Region {

    private int size;

    public Long getRequiredLongValue() {

        return Integer.toUnsignedLong(size);
    }
}

AbstractCountryProcess


public abstract class AbstractCountryProcess {

    CountryDAO cntDao;

    protected abstract boolean processCountry(Region region, City city);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM