我正在尝试测试一些使用反射的Java代码,作为测试的一部分,我需要创建一个与被测对象类型不同但共享相同(抽象)父级的对象(实际上,包装对象的可选
)。我正在测试以下形式的谓词:
abstractForm.isPresent() && (abstractForm.get().getClass() != this.getClass())
(不是我的设计 - 不要怪我!)并且需要为 abstractForm
创建一个模拟对象。如果我用 Mockito 创建它,只需使用 SacmElementciteElement = mock(SacmElement.class)
它实际上一切都很好除了我收到警告:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.cglib.core.ReflectUtils$2 (file:/C:/Users/owner/.m2/repository/org/mockito/mockito-core/1.10.19/mockito-core-1.10.19.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.mockito.cglib.core.ReflectUtils$2
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
所以它暂时有效,但不太可能继续有效。这是否超出了 Mockito 的能力范围,或者是否有一种未弃用的方法来获取 Mockito 模拟对象的类?
最佳答案
我在寻找答案时偶然发现了这个问题。我从this github issue意识到我使用的是旧版本的mockito。
我只是从这个版本更新
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
到这个版本
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.2.4</version>
<scope>test</scope>
</dependency>
问题在于 artifactId
已更改,并且 Google 在查找“mockito maven”时会向您发送旧 artifactId
的链接。
关于java - 如何将反射与 Mockito 模拟对象一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51160599/