If I'm not mistaken, you did everything right in your test case. The documentation of JMock about the with clause states
An expectation that uses parameter matchers must use the "with" method to wrap every parameter, whether a matcher function or a literal value.
The important part here is the emphasize on every
. You should only get the IllegalArgumentException
you mentioned,
java.lang.IllegalArgumentException: not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values
if you are mixing a with clause with a literal value - in your case e.g.
allowing(mockedFoo).bar(Class.class, with(any(Snafu.class)));
where Class.class
is the literal value. See also here.
I tested your code and it seems to work as expected. Here is my complete JUnit TestCase
:
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import junit.framework.TestCase;
public class FooTest extends TestCase{
Mockery context = new JUnit4Mockery();
public interface Foo {
public abstract <T> void bar(Class<? extends T> paramClass, T paramT);
}
public static class Snafu {}
public void testFoo() {
final Foo mock = context.mock(Foo.class);
context.checking(new Expectations() {
// keep warnings close to the culprit code when possible
@SuppressWarnings("unchecked")
public void allow(final Foo mockedFoo) {
allowing(mockedFoo).bar(
with(any(Class.class)), // Matcher that *should* resolve to Class<?>
with(any(Snafu.class))); // matcher to anything of type Snafu.class
}
{
allow(mock);
}
});
// test bar method (two invocations)
mock.bar(Snafu.class, someStaticFunctionThatReturnsASnafu());
mock.bar(Snafu.class, someStaticFunctionThatReturnsASnafu());
}
public static Snafu someStaticFunctionThatReturnsASnafu() {
return new Snafu();
}
}
This test case succeeds without any runtime exceptions (tested with JUnit 4 and JMock 2.6.0). I used with(any(Class.class))
instead of with(any(Snafu.class.getClass()))
for readability, but it doesn't really matter.
I only get the mentioned IllegalArgumentException
, if I change this to
allowing(mockedFoo).bar(Class.class, with(any(Snafu.class)));