在执行测试时(测试看起来像这样:http ://tomee.apache.org/examples-trunk/application-composer/README.html ),我总是得到:
java.lang.NoSuchMethodError: org.apache.openejb.jee.ManagedBean.<init>(Ljava/lang/String;Ljava/lang/String;Z)V
at org.apache.openejb.junit.ApplicationComposer$DeployApplication.evaluate(ApplicationComposer.java:194)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
我尝试使用 Maven 来获得所有依赖项。以下是依赖项列表:
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0-4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-jee</artifactId>
<version>4.0.0-beta-1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-persistence-jdbc</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-junit</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.4.2</version>
</dependency>
测试应该检查 EJB-DAO。它用于在数据库中写入实体并再次将其取出。这是测试代码:
import com.dlrg.dao.impl.RadioMessageDAOImp;
import com.dlrg.entity.RadioMessage;
import org.apache.openejb.jee.EjbJar;
import org.apache.openejb.jee.StatefulBean;
import org.apache.openejb.jee.jpa.unit.PersistenceUnit;
import org.apache.openejb.junit.ApplicationComposer;
import org.apache.openejb.junit.Configuration;
import org.apache.openejb.junit.Module;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import java.util.List;
import java.util.Properties;
@RunWith(ApplicationComposer.class)
public class RadioMessageDAOTest {
@EJB
private RadioMessageDAOImp radioMessageDAO;
@Resource
private UserTransaction userTransaction;
@PersistenceContext
private EntityManager entityManager;
@Configuration
public Properties config() throws Exception {
Properties p = new Properties();
p.put("MySQLtest", "new://Resource?type=DataSource");
p.put("MySQLtest.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("MySQLtest.JdbcUrl", "jdbc:hsqldb:file:data/hsqldb/hsqldb");
return p;
}
@Module
public PersistenceUnit persistence() {
PersistenceUnit unit = new PersistenceUnit("radio-unit");
unit.setJtaDataSource("MySQLtest");
unit.setNonJtaDataSource("radioDatabaseUnmanaged");
unit.getClazz().add(RadioMessage.class.getName());
unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
return unit;
}
@Module
public EjbJar beans() {
EjbJar ejbJar = new EjbJar("radio-beans");
ejbJar.addEnterpriseBean(new StatefulBean(RadioMessageDAOImp.class));
return ejbJar;
}
@Test
public void test() throws Exception {
userTransaction.begin();
try {
entityManager.persist(new RadioMessage());
List<RadioMessage> list = radioMessageDAO.getAll();
Assert.assertEquals(1, list.size());
for (RadioMessage movie : list) {
radioMessageDAO.delete(movie);
}
Assert.assertEquals(0, radioMessageDAO.getAll().size());
} finally {
userTransaction.commit();
}
}
}