我使用的是Mockito2.23。4和字节1.9。3在我的项目中。我想模拟最终的类和方法,所以我使用了mockito inline,这篇文章后面是mockito mock final类和方法,并在org内部设置
文件位于项目的mock maker inline
。莫基托。插件。MockMakersrc/test/resources/mockito extensions
目录中。我还跟踪了post Mockito失败,启用了内联mock,参数名异常无效,但问题仍然存在。但是当我运行测试时,我得到了下面的堆栈跟踪。
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
Test ignored.
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class org.elasticsearch.client.RestHighLevelClient.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 15
JVM vendor name : Oracle Corporation
JVM vendor version : 15.0.2+7-27
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 15.0.2+7-27
JVM info : mixed mode, sharing
OS name : Windows 10
OS version : 10.0
You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.
Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [class org.elasticsearch.client.RestHighLevelClient, class java.lang.Object, interface java.io.Closeable, interface java.lang.AutoCloseable]
at org.mockito.internal.runners.DefaultInternalRunner$1.withBefores(DefaultInternalRunner.java:39)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.mockito.exceptions.base.MockitoException: Could not modify all classes [class org.elasticsearch.client.RestHighLevelClient, class java.lang.Object, interface java.io.Closeable, interface java.lang.AutoCloseable]
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:152)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376)
... 19 more
Caused by: java.lang.IllegalStateException:
Byte Buddy could not instrument all classes within the mock's type hierarchy
This problem should never occur for javac-compiled classes. This problem has been observed for classes that are:
- Compiled by older versions of scalac
- Classes that are part of the Android distribution
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:164)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClass(InlineBytecodeGenerator.java:141)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:37)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:34)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:152)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMockType(InlineByteBuddyMockMaker.java:200)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMock(InlineByteBuddyMockMaker.java:181)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:69)
at org.mockito.Mockito.mock(Mockito.java:1896)
at org.mockito.internal.configuration.MockAnnotationProcessor.processAnnotationForMock(MockAnnotationProcessor.java:44)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:19)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:38)
at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:57)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
... 19 more
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 59
at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:184)
at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:166)
at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:152)
at net.bytebuddy.utility.OpenedClassReader.of(OpenedClassReader.java:82)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForInlining.create(TypeWriter.java:3390)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:1930)
at net.bytebuddy.dynamic.scaffold.inline.RedefinitionDynamicTypeBuilder.make(RedefinitionDynamicTypeBuilder.java:207)
at net.bytebuddy.dynamic.scaffold.inline.AbstractInliningDynamicTypeBuilder.make(AbstractInliningDynamicTypeBuilder.java:116)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:3394)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.transform(InlineBytecodeGenerator.java:215)
at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:563)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:161)
... 40 more
我尝试了字节伙伴的以下依赖项设置,正如在使用内联mock失败的post mockito中提到的:
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>LATEST</version>
</dependency>
但是仍然没有成功解决这个错误。我的测试代码和实际方法在我之前的帖子中提到:当试图在Mockito[复制]中模拟ElasticClient时,会出现NPE错误。
下一步我能试试什么?
您需要更新您的依赖项。
您正在使用jdk15编译您的项目。ByteBuddy抱怨它不能测试这个jdk生成的类文件(不支持的类文件主要版本59)
ByteBuddy 1.10.6版本中引入了对java 15的支持
请注意,您不需要在项目中指定ByteBuddy的版本,因为Mockito可以通过传递方式获取它(除非您有充分的理由强制使用一个版本)。
Mockito 3.3中引入了兼容的ByteBuddy版本。10.参见提交将ByteBuddy版本更改为1.10。10
与分析Mockito中提交的历史不同,我建议使用最新和最伟大的(撰写本文时为3.8.0)
我正在使用ModelMapper进行NPE 目录服务测试 目录服务 目录映射器 目录库 按预期返回CatalogEntity对象,问题出现在执行返回null之后。
我试图使用Mockito的Spy部分模拟一个服务,重写一个方法,使其返回一致的数据进行测试,但Say Spy无缘无故抛出一个UnfinishedStubbingException。 这是我的测试类: 这两个测试都失败了,指向指向
在我的项目中,我们已经有mockito核心依赖项。我想存根静态方法,我需要为其添加mockito内联依赖项。所以想了解它们之间的区别。它们可以共存吗?
我在测试跟随功能。 下面是测试代码。 我定义了以下变量。 问题是不会引发异常。所以测试一直失败。 请告诉我我缺少的东西。
我使用PowerMock(Mockito)模拟同一类中另一个方法的子调用。更具体地说,我有这样的东西: 现在在我的单元测试中,我能够使用间谍模拟MyMethod2的响应,并做一些类似。但是,当我做这样的事情时,会发生一些奇怪的事情:。当我在测试期间调用myClass.myMethod1()时,它会抛出一个NullPointerException,但奇怪的是,如果我使用调试器并检查,e是MyExce
我试图模拟最后一个类的静态方法:Files。以身作则。 我读到我必须用户一个mock制造商内联mockito插件,所以我设置它: 我把这个: 在:src/test/Resources/mockito扩展/org.mockito.plugins.MockMaker 然后我模拟了我的类并尝试验证我的模拟是否被调用: 我有一个例外: 我在mockito 2.7.22中使用了power mockito 2