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)?
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With