Home > Back-end >  Avoid compiler warning using wildcard for primitive types in Spock
Avoid compiler warning using wildcard for primitive types in Spock

Time:07-22

I'm using Spock Framework for testing my Java application. The compiler warns me whenever I try to instrument a Mock using a wildcard for a primitive type.

For example (trivial example):

service.add(_, _) >> 42

Both the above parameters are primitive int. The compiler gives me the following warning:

Warning:(34, 30) 'add' in 'Service' cannot be applied to '(java.lang.Object, java.lang.Object)'

At runtime, the test passed without any problem. However, if I try to specify the type of wildcards, the test fails.

How can I avoid the above warning message?

CodePudding user response:

You should use type wrappers instead of primitives.

See the project at github.com/jeffbrown/riccardocardin.

lib/src/test/groovy/riccardocardin/LibraryTest.groovy#L9 works.

1 * add(_ as Integer, _ as Integer) >>  2112

github.com/spockframework/spock/issues/669 explains why this is not a bug.

  • Related