我在一次测试中试图使用一个模拟类,但出现了以下错误:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.example.Car.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.161-b12
JVM name : Java HotSpot(TM) Client VM
JVM version : 1.8.0_161-b12
JVM info : mixed mode, sharing
OS name : Windows 10
OS version : 10.0
Underlying exception : java.lang.IllegalArgumentException: Could not create type
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:43)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
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:74)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
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:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Could not create type
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:139)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:344)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:159)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:353)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:71)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:42)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.mock(Mockito.java:1908)
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:49)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
... 21 more
Caused by: java.lang.NoSuchMethodError: net.bytebuddy.dynamic.loading.MultipleParentClassLoader$Builder.appendMostSpecific(Ljava/util/Collection;)Lnet/bytebuddy/dynamic/loading/MultipleParentClassLoader$Builder;
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:83)
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:137)
... 39 more
在pom中:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.28.2</version>
<scope>test</scope>
</dependency>
(也试用了3.0.0版)
车辆等级:
public class Car{
// Some properties. Non of them is final / static.
// constructor
}
测试类:
@RunWith(MockitoJUnitRunner.class)
public class ParkinglotTest{
@Mock
private Car car;
private Parkinglog parkinglot;
@Before
public void setUp(){
parkinglot = new Parkinglot(car);
}
....
在降级Mockito版本后,我能够修复该问题:
<代码>
在我的例子中,这是因为它引用的是另一个版本的byte-buddy,它实际上与mockito库不兼容。
这个附加版本的byte好友,来自hibernate-core(版本5.3.7),它也存在于我的类路径中。
最初,我使用的是mockito-core库version-3.3.3,它实际上需要byte-Buddy 1.10.5。
因此,我将mockito核心库降级为2.21.0版本,该版本使用byte buddy版本1.8.15。
它开始对我起作用了。
对我来说,另一个解决方案是更新到更新版本的hibernate(版本
但这可能会对项目产生更大的影响,所以我选择了第一个解决方案,具体如下,具体取决于您使用的库:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.21.0</version>
<scope>test</scope>
</dependency>
或
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.21.0</version>
<scope>test</scope>
</dependency>
此外,这个问题在这里得到了解决:https://github.com/mockito/mockito/issues/1606帮助我找到了解决方案。
似乎这可能是因为找不到安装的bytebuddy,指的是:
原因:java。lang.NoSuchMethodError:net。拜特巴迪。动态加载。MultipleParentClassLoader$生成器。appendMostSpecific(Ljava/util/Collection;)Lnet/bytebuddy/dynamic/loading/MultipleParentClassLoader$Builder;
从我在网上找到的内容来看,将bytebuddy添加到依赖项可能会解决这个问题。
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>XXX</version>
</dependency>
我试图使用PowerMockito在我正在测试的代码中模拟java.net.URL类的创建。基本上,我希望防止真正的HTTP请求发生,而是1)在请求发出时检查数据,2)在模拟响应中提供我自己的测试数据。这就是我正在尝试的: 我要测试的代码如下所示: 在前面的测试场景中,我模拟了wlInvokeUrlString以匹配“MyURLString”。我还尝试使用whenNew行的各种其他形式,尝试注入模
给定类别FOO: 我尝试使用JDK8而不是9,它工作得很好。我不知道它为什么会失败,也不知道我怎么才能让它成功。 当是依赖项时,就会发生此问题。如果您为其他类型(如)更改它,它可以正常工作...
我无法使用mockito2模拟一个Kotlin final类。另外我还在用Robolectric。 这是我的测试代码: 请注意,我使用的是Mockito Version2,我使用的是依赖项,它自动启用模拟最终类的功能。
我想在类AppleProcessor中测试一个方法: 请注意,Fruit是AppleFruit实现的方法的接口,并且还有一个方法。 我的测试看起来像: 然而,我得到了以下错误: