1

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?

asked Apr 30, 2019 at 16:02
3
  • 2
    Is cntDao actually mocked in your tests? Give a minimal reproducible example. Commented Apr 30, 2019 at 16:04
  • @jonrsharpe - Yes sorry I have updated question Commented Apr 30, 2019 at 16:09
  • when using the JUnitRuleMockery you may better use the @Mock annotation instead of direct call to context for creating a mock. Of cause this implies tat the variable holding the mock is not static. Commented May 2, 2019 at 10:48

2 Answers 2

2

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.

answered Apr 30, 2019 at 20:51
Sign up to request clarification or add additional context in comments.

2 Comments

Fair. Looks like you have a point but could you give me an example on current code context. It will definitely help
Updated the answer by mentioning Mockito.mock
1

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);
}
answered Sep 28, 2021 at 5:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.