sreedharkesarla / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

NPE when Mocking, sending a null to array argument #245

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Source code:

def "show problem"() {
        given:
        SampleFactory sf = Mock()

        when:
        sf.makeObject("b", null);

        then:
        0 * sf.makeObject("a", _);
    }

    private interface SampleFactory {
        void makeObject(String id, Integer[] arrayInt)
    }

Exception thrown:
java.lang.NullPointerException
    at java.util.Arrays$ArrayList.<init>(Arrays.java:3357)
    at java.util.Arrays.asList(Arrays.java:3343)
    at org.spockframework.mock.PositionalArgumentListConstraint.expandVarArgs(PositionalArgumentListConstraint.java:57)

Reason:
PositionalArgumentListConstraint.java line 57
 expanded.addAll(Arrays.asList((Object[]) CollectionUtil.getLastElement(args)));
1. Because the last element of method should be an array, Spock checks for Var 
Arg Syntax
2. The actual value for last element is null. Hence, Arrays.asList fails

What version of Spock and Groovy are you using?
Spock 0.6 Groovy 1.7.10

Original issue reported on code.google.com by cyprush...@gmail.com on 3 Apr 2012 at 12:40

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 12 May 2012 at 12:08