Ben Siver
Ben Siver

Reputation: 2948

Allowing Object or null in jMock expectations

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?

Upvotes: 8

Views: 2399

Answers (1)

Ben Siver
Ben Siver

Reputation: 2948

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.

Upvotes: 7

Related Questions