gpt4 book ai didi

java - 任何(MyClass.class)实际上只匹配传递类类型的类?

转载 作者:搜寻专家 更新时间:2023-10-31 19:57:32 25 4
gpt4 key购买 nike

我有以下代码:

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

我的代码如下:

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

代码失败,因为第一次验证似乎发现有 2 次调用 javaCompiler.writeJavaAndCompile()。它没有意识到只有一个 ContractCompilationUnit 类型的调用。

避免这种行为的标准程序是什么(除了必须编写我自己的匹配器)?

最佳答案

documentation显示这是已知行为:

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.

看起来您应该改用 isA:

verify(javaCompiler).writeJavaAndCompile(isA(ContractCompilationUnit.class),
eq(outputDirectory));

关于java - 任何(MyClass.class)实际上只匹配传递类类型的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9358308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com