Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing Object or null in jMock expectations

Tags:

java

junit

jmock

I recently upgraded from jMock 2.5.1 to 2.6.0, and it seems that some of its dependencies have changed, causing some of my previously passing tests to fail.

One of my tests has the following expectation that used for the common setup of several tests:

oneOf(service).event(with(any(Long.class)));

In my test suite, event gets called with both null and valid Long values. This used to be perfectly acceptable in jMock 2.5.1, but after the upgrade, I get the following exception:

java.lang.AssertionError: unexpected invocation: service.event(null)
expectations:
  expected once, never invoked: service.event(an instance of java.lang.Long)
what happened before this:
  locator.locateService()
  service.getService()
at org.jmock.api.ExpectationError.unexpected(ExpectationError.java:23)
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:85)
at org.jmock.Mockery.dispatch(Mockery.java:231)
at org.jmock.Mockery.access$100(Mockery.java:29)
at org.jmock.Mockery$MockObject.invoke(Mockery.java:271)
at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
at org.jmock.lib.concurrent.Synchroniser.synchroniseInvocation(Synchroniser.java:82)
at org.jmock.lib.concurrent.Synchroniser.access$000(Synchroniser.java:23)
at org.jmock.lib.concurrent.Synchroniser$1.invoke(Synchroniser.java:74)
at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
at com.sun.proxy.$Proxy27.system(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

I suspect this might be due to the new version of Hamcrest that jMock 2.6.0 uses, but I'm unsure. Is there a more suitable matcher I can use to specify both null and non-null values for this method?

like image 833
Ben Siver Avatar asked Feb 23 '15 16:02

Ben Siver


People also ask

What are expectations in jmockit?

An expectation represents a set of invocations to a specific mocked method/constructor that is relevant for a given test. An expectation may cover multiple different invocations to the same method or constructor, but it doesn't have to cover all such invocations that occur during the execution of the test.

What is JMock in Java?

JMock is a library that supports test-driven development of Java code with mock objects . Mock objects help you design and test the interactions between the objects in your programs. The jMock library: makes it quick and easy to define mock objects, so you don't break the rhythm of programming.


1 Answers

After doing a bit more research, I learned this is a known change in functionality from jMock 2.5 -> 2.6.

The workaround I discovered is to use with.is(anything()), which matches null and non-null values.

like image 103
Ben Siver Avatar answered Sep 27 '22 15:09

Ben Siver