我在编译单元测试时遇到问题。我收到以下错误消息。
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/event/ValueChangeEvent
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:143)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethod(Class.java:1935)
at net.sf.cglib.proxy.Enhancer.getCallbacksSetter(Enhancer.java:630)
at net.sf.cglib.proxy.Enhancer.isEnhanced(Enhancer.java:604)
at org.powermock.api.extension.proxyframework.ProxyFrameworkImpl.isProxy(ProxyFrameworkImpl.java:29)
at org.powermock.reflect.internal.WhiteboxImpl.getUnmockedType(WhiteboxImpl.java:1783)
at org.powermock.reflect.internal.WhiteboxImpl.getConstructor(WhiteboxImpl.java:248)
at org.powermock.core.MockGateway.newInstanceCall(MockGateway.java:192)
at br.gov.dataprev.siaci.web.ConsultaProcessosTest.setUp(ConsultaProcessosTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:129)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:93)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
当我使用 PowerMock 时会发生此错误。有没有人遇到过这个问题?
这不会发生在特定的点代码上。在编译测试类时发生。这是我的测试课。
@RunWith(PowerMockRunner.class)
@PrepareForTest(BeanUtil.class)
public class ConsultaProcessosTest {
private ConsultaProcessos consulta;
private ServicoConsultaProcessos mockServicoConsulta;
private Acordo acordo;
@Before
public void setUp() {
consulta = new ConsultaProcessos();
mockServicoConsulta = createMock(ServicoConsultaProcessos.class);
consulta.setServicoConsultaProcessos(mockServicoConsulta);
acordo = Util.criarAcordoComDadosMinimos("1");
consulta.setAcordo(acordo);
}
@Test
public void consultaComResultadosDeveIrParaPaginaDeResultados() throws Exception {
List<ProcessoTO> resultadoConsulta = new ArrayList<ProcessoTO>();
resultadoConsulta.add(new ProcessoTO());
ParametrosConsultaProcessos parametrosConsultaProcessos = consulta.obterParametrosConsultaProcessos();
expect(mockServicoConsulta.consultarProcessos(parametrosConsultaProcessos)).andReturn(resultadoConsulta);
replay(mockServicoConsulta);
PowerMock.mockStatic(BeanUtil.class);
EasyMock.expect(BeanUtil.salvarValorRequisicao("resultado", resultadoConsulta)).andReturn(resultadoConsulta);
PowerMock.replay(BeanUtil.class);
String resultado = consulta.consultarProcessos(parametrosConsultaProcessos);
String resultadoEsperado = "/pages/consulta/resultadoConsultaProcessos.xhtml";
assertEquals(resultadoEsperado, resultado);
verify(mockServicoConsulta);
PowerMock.verify(BeanUtil.class);
}
}
注释掉这个:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
替换为以下依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
(如果您正在使用 GlassFish 以外的其他应用服务器,您可能还需要这个)
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
参考: http://forum.springsource.org/showthread.php?57108-ClassFormatError-Absent-Code-attribute