问题描述
这是我的JUnit测试用例框架:
package com.xxx.indexer.scheduler.service;
import java.util.Properties;
import javax.ejb.embeddable.EJBContainer;
import javax.naming.NamingException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.xxx.indexer.scheduler.AbstractTest;
public class BookingAuditServiceImplTest extends AbstractTest {
private static EJBContainer container;
@BeforeClass
public static void start(){
final属性props = new Properties();
props.setProperty(EJBContainer.PROVIDER,tomee-embedded);
container = EJBContainer.createEJBContainer(props);
}
@AfterClass
public static void stop(){
container.close();
}
@Before
public void inject()throws NamingException {
container.getContext()。bind(inject,this);
}
@Test
public void test(){
// TODO
}
}
这些是我的maven依赖:
<依赖性>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.10< / version>
< scope> test< / scope>
< / dependency>
<依赖关系>
< groupId> javax< / groupId>
< artifactId> javaee-api< / artifactId>
< version> 6.0< / version>
< scope>已提供< / scope>
< / dependency>
<依赖关系>
< groupId> org.apache.openejb< / groupId>
< artifactId> tomee-embedded< / artifactId>
< version> 1.0.0< / version>
< scope> test< / scope>
< / dependency>
当我启动测试类时,EJBContainer.createEJBContainer()方法抛出一个java.lang.ClassFormatError例外:
java.lang.ClassFormatError:在类文件中不是本机或抽象的方法中缺少代码属性javax / ejb / java.lang.ClassLoader.defineClass1(Native Method)中的可嵌入/ EJBContainer
在java.lang.ClassLoader.defineClass(ClassLoader.java:791)
在java.security.SecureClassLoader.defineClass (SecureClassLoader.java:142)
在java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
在java.net.URLClassLoader.access $ 100(URLClassLoader.java:71)
在java.net.URLClassLoader $ 1.run(URLClassLoader.java:361)
在java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)
在java.security.AccessController.doPrivileged(本机方法)
在java.net.URLClassLoader.findClass(URLClassLoader.java:354)
在java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.La uncher $ AppClassLoader.loadClass(Launcher.java:308)
在java.lang.ClassLoader.loadClass(ClassLoader.java:356)
在java.lang.Class.getDeclaredFields0(本机方法)
在java.lang.Class.privateGetDeclaredFields(Class.java:2308)
在java.lang.Class.getDeclaredFields(Class.java:1760)
在org.junit.runners.model.TestClass。 < init>(TestClass.java:44)
在org.junit.runners.ParentRunner。< init>(ParentRunner.java:73)
在org.junit.runners.BlockJUnit4ClassRunner。 init>(BlockJUnit4ClassRunner.java:55)
在org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
在org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder .java:57)
在org.junit.internal.builders.AllDefaultPossabilitiesBuilder.runnerForClass(AllDefaultPossabilitiesBuilder.java:29)
在org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57 )
在org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java :24)
在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference。< init>(JUnit4TestReference.java:33)
在org.eclipse.jdt.internal.junit4.runner。 JUnit4TestClassReference。< init>(JUnit4TestClassReference.java:25)
在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
在org.eclipse.jdt .internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
在org .eclipse.rar $ b在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我如何做?
我在这个链接中阅读,问题是maven依赖的顺序。
我必须先声明tomee嵌入的工件,并在javaee api之后,以这种方式:
< dependency>
< groupId> org.apache.openejb< / groupId>
< artifactId> tomee-embedded< / artifactId>
< version> 1.0.0< / version>
< scope> test< / scope>
< / dependency>
<依赖关系>
< groupId> javax< / groupId>
< artifactId> javaee-api< / artifactId>
< version> 6.0< / version>
< scope>已提供< / scope>
< / dependency>
它的作品。
I would test my EJB with TomEE embedded EJBContainer.
This is my JUnit test case skeleton:
package com.xxx.indexer.scheduler.service;
import java.util.Properties;
import javax.ejb.embeddable.EJBContainer;
import javax.naming.NamingException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.xxx.indexer.scheduler.AbstractTest;
public class BookingAuditServiceImplTest extends AbstractTest {
private static EJBContainer container;
@BeforeClass
public static void start() {
final Properties props = new Properties();
props.setProperty(EJBContainer.PROVIDER, "tomee-embedded");
container = EJBContainer.createEJBContainer(props);
}
@AfterClass
public static void stop() {
container.close();
}
@Before
public void inject() throws NamingException {
container.getContext().bind("inject", this);
}
@Test
public void test() {
// TODO
}
}
And these are my maven dependecies:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-embedded</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
When I start the test class, the EJBContainer.createEJBContainer() method throws a java.lang.ClassFormatError exception:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ejb/embeddable/EJBContainer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2308)
at java.lang.Class.getDeclaredFields(Class.java:1760)
at org.junit.runners.model.TestClass.<init>(TestClass.java:44)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
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)
How can I do?
I read at this link, https://stackoverflow.com/a/3416368/1412839, that the problem is the order of maven dependencies.
I have to declare first the tomee embedded artifact, and after the javaee api, in this way:
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>tomee-embedded</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
It works.
这篇关于EJB测试用TomEE嵌入EJBContainer api:java.lang.ClassFormatError异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!