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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With