Maven, JUnit, OpenEJB, ApplicationComposer leads t

2019-08-26 16:41发布

问题:

While executin the test (test looks like this: http://tomee.apache.org/examples-trunk/application-composer/README.html), I always get:

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)

I try to use Maven in order to get all the dependencies right. Here is the list of dependencies:

<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>

The test should check an EJB-DAO. It is used to write an Entity in the DB and get it out of it again. Here is the test code:

    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();
        }
    }
}

回答1:

Rather use

org.apache.openejb openejb-jee 4.5.0

You are mixing a beta version and version 4.5.0 of the OpenEjb stuff. That can't work.