我想删除 1 类的最终关键词。我正在为这个类编写 Junit,并且我调用 Mockito 的 spy 来模拟一个方法来返回一个期望值以涵盖所有情况。
使用后:
CtClass ctClazz = ClassPool.getDefault().get(Livre.class.getName());
ctClazz.setModifiers(ctClazz.getModifiers() & ~Modifier.FINAL);
ctClazz.writeFile();
这个类的修饰符是 17,但我认为它应该是 1(公共)。
我得到了错误:
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class mediatheque.document.Livre
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
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.powermock.api.easymock.internal.signedsupport.SignedSupportingClassProxyFactory.createProxy(SignedSupportingClassProxyFactory.java:147)
at org.easymock.internal.MocksControl.createMock(MocksControl.java:40)
at org.easymock.classextension.internal.MocksClassControl.createMock(MocksClassControl.java:43)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2076)
at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:79)
at org.powermock.api.easymock.PowerMock.createPartialMock(PowerMock.java:765)
at test.mediatheque.document.TestDocument.testRestituer_With_Exception3(TestDocument.java:379)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$2.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:217)
... 19 more
这个最终类从一个抽象类扩展而来。
谢谢你的帮助。