Using jmockit expectations with combos and primitive types

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?

+4
source share
3 answers

So the problem arises in Expectations.with ():

  protected final <T> T with(Matcher<T> argumentMatcher) { argMatchers.add(argumentMatcher); TypeVariable<?> typeVariable = argumentMatcher.getClass().getTypeParameters()[0]; return (T) Utilities.defaultValueForType(typeVariable.getClass()); } 

A call of type Variable.getClass () does not fulfill what the author expects, and a call of type Utilities.defaultValueFor returns null. De-auto-back boxing of a primitive logical value is the place from which NPE originates.

I fixed this by changing the invokeReturning (...) call to:

 invokeReturning(withEqual(false)), "bas"); 

I no longer use the helper, but it is good enough for what I need.

+1
source

the problem is a combination of using Expectations and that Matchers do not support a primitive type.

Matchers code relies on Generic, which basically does not support the primitive type. As a rule, using Matchers is more suitable for matching values; With auto-boxing / unboxing feater in Java 5, this is usually not a problem.

But JMockit Expectation doesn’t use it to match the value, it uses it for some kind of parsing to determine the type of the method signature..which in this case the Matchers will be boolean, while your method is a primitive type. therefore he cannot scoff at it.

I'm sorry that I can not tell you about this. Maybe someone can help.

+1
source

I changed JMockit (release 0.982) so that “with (is (false))” and other similar options now work as expected (it no longer returns null, but the actual value of the argument inside the inner connector).

+1
source

Source: https://habr.com/ru/post/1276836/


All Articles