I use jmockit for unit testing (with TestNG), and I'm having problems using the Expectations class to retrieve a method that uses a primitive type (boolean) as a parameter, using a match. Here is sample code that illustrates the problem.
import static org.hamcrest.Matchers.is; import mockit.Expectations; import org.testng.annotations.Test; public class PrimitiveMatcherTest { private MyClass obj; @Test public void testPrimitiveMatcher() { new Expectations(true) { MyClass c; { obj = c; invokeReturning(c.getFoo(with(is(false))), "bas"); } }; assert "bas".equals(obj.getFoo(false)); Expectations.assertSatisfied(); } public static class MyClass { public String getFoo(boolean arg) { if (arg) { return "foo"; } else { return "bar"; } } } }
The string containing the invokeReturning (...) call throws a NullPointerException.
If I change this call so as not to use a match, as in:
invokeReturning(c.getFoo(false), "bas");
It works great. This is not good for me, because in my real code, I actually make fun of a multi-parameter method, and I need to use a match for another argument. In this case, the Expectations class requires that all arguments use a match.
I'm sure this is a mistake, or it might not be possible to use Matches with primitive types (this upset me). Has anyone encountered this problem and know how to get around it?
source share