Okay, basically it looks like it's a bug in JUnitParams. After applying the fix in the original answer below, the library still "unwraps" the String[]
when we don't want it to - given the amount of conditional wrapping and unwrapping going on in the source, it seems that either the author is trying to be too clever for their own good, or they don't really know what they're expecting at any one time. The difference occurs in safelyCastParamsToArray
, where in the int[]
case it's passed an int[]
and wraps it in an Object[]
, whereas in the String[]
cases it's passed a String[]
which is not wrapped because it can be cast to Object[]
already.
You can make it work by making your method return a doubly-wrapped array, like this:
return $((Object)$((Object)getStringArray()));
Or just:
return new Object[] { new Object[] { getStringArray() } };
The int[]
version only needs a single level of wrapping however, so you can just use:
return $(getIntArray());
rather than the "attempted double wrapping which is foiled by varargs" in your current code.
Personally I'm somewhat wary of all of this - it has a distinctly "black magic, tinker until it works" feeling which doesn't tend to be present in clean APIs...
Original answer
I believe the problem is that a String[]
array is already an Object[]
, so you're ending up with it not being wrapped in the way that you want it to - varargs is treating the argument as as the whole array rather than one element of the array. You can fix this fairly easily though:
public Object getStrings() {
Object array = getStringArray();
return $(array);
}
Or just use a cast:
public Object getStrings() {
return $((Object) getStringArray());
}
Or just ignore the $
method in this case:
public Object getStrings() {
return new Object[] { getStringArray() };
}
(I very much doubt that you need $($(...))
in either case - you're just trying to make an array of arrays, right? So only a single level of wrapping is required, and you're only getting a single level of wrapping anyway, for precisely the same reason that your original code didn't work with the string array.)