Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

any(MyClass.class) that actually matches only classes of the type of the passed class?

I have the following code:

verify(javaCompiler, times(1)).writeJavaAndCompile(any(ContractCompilationUnit.class), eq(outputDirectory));
verify(javaCompiler, times(1)).writeJavaAndCompile(any(ParamCompilationUnit.class), eq(outputDirectory));       

and my code is the following:

javaCompiler.writeJavaAndCompile(new ContractCompilationUnit(), outputDirectory);
javaCompiler.writeJavaAndCompile(new ParamCompilationUnit(), outputDirectory);

The code is failing, as it seems that the 1st verify sees that there were 2 calls to javaCompiler.writeJavaAndCompile(). It is failing to realize that there was only one call of type ContractCompilationUnit type.

What's the standard procedure to avoid this behaviour (other than having to write my own matcher)?

like image 934
devoured elysium Avatar asked Feb 20 '12 08:02

devoured elysium


1 Answers

The documentation shows that this is the known behaviour:

Any kind object, not necessary of the given class. The class argument is provided only to avoid casting. Sometimes looks better than anyObject() - especially when explicit casting is required

Alias to anyObject()

This method don't do any type checks, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.

It looks like you should use isA instead:

verify(javaCompiler).writeJavaAndCompile(isA(ContractCompilationUnit.class),
                                         eq(outputDirectory));
like image 105
Jon Skeet Avatar answered Oct 05 '22 04:10

Jon Skeet