EasyMock 使用 clone() 方法为 class 创建模拟失败
EasyMock create mock fails for a class with a clone() method
我在尝试模拟具有 clone() 方法的 class 时遇到问题,该方法会创建对象的深层副本。当我删除此方法时,模拟工作正常。
public class ConcreteClass implements Iterable<Record> {
@Override
public Iterator<Record> iterator() {
}
public ConcreteClass clone() {
ConcreteClass copy = new ConcreteClass();
return copy;
}
使用 EasyMock.createMock(ConcreteClass.class)
创建上述 class 的模拟失败并出现以下异常
java.lang.ArrayIndexOutOfBoundsException: 15092
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.proxy.BridgeMethodResolver.resolveAll(BridgeMethodResolver.java:61)
at net.sf.cglib.proxy.Enhancer.emitMethods(Enhancer.java:911)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:498)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
at org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:175)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:129)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:103)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:94)
at org.easymock.EasyMock.createMock(EasyMock.java:136)
at com.test.Test(Test.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
然而,当我删除 clone() 方法时,我可以成功创建模拟。
我正在使用 EasyMock 3.x
指定完整的 EasyMock 版本是个好主意。我刚刚尝试使用 EasyMock 4.0.2,它没有任何问题。所以升级可能是答案。
请注意 clone()
是 Java 的特殊名称。用户通常会期望 class 为 Cloneable
,覆盖为 return Object
(出于许多遗留原因)而不是实际的 class。实现应该调用 super.clone()
。如果我是你,我会将方法重命名为 copy()
.
我在尝试模拟具有 clone() 方法的 class 时遇到问题,该方法会创建对象的深层副本。当我删除此方法时,模拟工作正常。
public class ConcreteClass implements Iterable<Record> {
@Override
public Iterator<Record> iterator() {
}
public ConcreteClass clone() {
ConcreteClass copy = new ConcreteClass();
return copy;
}
使用 EasyMock.createMock(ConcreteClass.class)
创建上述 class 的模拟失败并出现以下异常
java.lang.ArrayIndexOutOfBoundsException: 15092
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.asm.ClassReader.<init>(Unknown Source)
at net.sf.cglib.proxy.BridgeMethodResolver.resolveAll(BridgeMethodResolver.java:61)
at net.sf.cglib.proxy.Enhancer.emitMethods(Enhancer.java:911)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:498)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
at org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:175)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:129)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:103)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:94)
at org.easymock.EasyMock.createMock(EasyMock.java:136)
at com.test.Test(Test.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
然而,当我删除 clone() 方法时,我可以成功创建模拟。 我正在使用 EasyMock 3.x
指定完整的 EasyMock 版本是个好主意。我刚刚尝试使用 EasyMock 4.0.2,它没有任何问题。所以升级可能是答案。
请注意 clone()
是 Java 的特殊名称。用户通常会期望 class 为 Cloneable
,覆盖为 return Object
(出于许多遗留原因)而不是实际的 class。实现应该调用 super.clone()
。如果我是你,我会将方法重命名为 copy()
.