javax.ejb.EJBException:javax.ejb.CreateException:无法创建无状态EJB junit

时间:2016-06-04 12:05:25

标签: java java-ee junit glassfish ejb

我正在学习EJB,当试图用junit测试它时,我得到以下错误

cd.espoirmur.Ejb.InterfaceEjbLocal_80488159
Jun 03, 2016 10:33:58 AM com.sun.ejb.containers.StatelessSessionContainer createStatelessEJB
SEVERE: ejb.stateless_ejbcreate_exception
Jun 03, 2016 10:33:58 AM com.sun.ejb.containers.BaseContainer postInvoke
WARNING: A system exception occurred during an invocation on EJB BookEjb, method: public java.lang.Object cd.espoirmur.Ejb.BookEjb.enregistrer(java.lang.Object)
Jun 03, 2016 10:33:58 AM com.sun.ejb.containers.BaseContainer postInvoke
WARNING: 
javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:435)
    at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2579)
    at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1971)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:210)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy161.enregistrer(Unknown Source)
    at cd.espoirmur.test.BookEjbTest.createBook(BookEjbTest.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:700)
    at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:246)
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:430)
    ... 37 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:514)
    at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:97)
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:698)
    ... 39 more
Caused by: java.lang.reflect.InvocationTargetException
    at com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1706)
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:456)
    ... 41 more
Caused by: java.lang.NullPointerException
    at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
    at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:1273)
    at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:178)
    at org.glassfish.weld.services.JCDIServiceImpl._createJCDIInjectionContext(JCDIServiceImpl.java:198)
    at org.glassfish.weld.services.JCDIServiceImpl.createJCDIInjectionContext(JCDIServiceImpl.java:179)
    at com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1696)
    ... 42 more

PlainTextActionReporterSUCCESSNo monitoring data to report.
JdbcRuntimeExtension,  getAllSystemRAResourcesAndPools = [GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource]
Jun 03, 2016 10:33:59 AM org.glassfish.admin.mbeanserver.JMXStartupService shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
Jun 03, 2016 10:33:59 AM org.glassfish.admin.mbeanserver.JMXStartupService shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
JdbcRuntimeExtension,  getAllSystemRAResourcesAndPools = [GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource]

这是我的测试代码:

public class BookEjbTest {
private static EJBContainer ec;
private static Context ctx;
@BeforeClass
public static void initcontainer() throws Exception {
    File module = new File("target/classes/cd/espoirmur/ejb");
    Map<String, Object> properties = new HashMap<>();
    properties.put(EJBContainer.MODULES, module);
    ec = EJBContainer.createEJBContainer(properties);
    System.out.println("--------------ejb container sucessfully created----------" + Arrays.toString(module.listFiles()) + "---------liste des moduless");
    ctx = ec.getContext();
    System.out.println("--------------ejb context successfull  sucessfully created----------");
}

@AfterClass
public static void closeContainer() throws Exception {
    ec.close();
}

@Test
public void createBook() throws Exception {
    Book book = new Book();
    long id = 12309;
    book.setId(id);
    book.setDescription("mon premier livre sur les EJB");
    book.setIllustrations(Short.MIN_VALUE);
    book.setIsbn("ISBN-129-OK");
    book.setNbofpage(123);
    book.setBookTitle("Debutez avec les EJB");
    book.setPrice(12.5);
    System.out.println("-----------begin checking--------");
    InterfaceEjbLocal bookejb = (InterfaceEjbLocal ) ctx.lookup("java:global/ejb/BookEjb");
    if (bookejb != null) {
        System.out.println(bookejb.toString());
        bookejb.enregistrer(book);
        assertNotNull("ID should not be null", book.getId());
        List<Book> books = bookejb.find();
        assertNotNull(books);
    } else {
        System.out.println("-----------null object return--------");
    }

}

}

ejb代码:

    @Local(InterfaceEjbLocal.class)
@Stateless
public class BookEjb implements Serializable,InterfaceEjbLocal<Book>{
    @PersistenceContext(unitName = "BookStorePU")
    private EntityManager em;
    @Override
    public List<Book> find() {
        List<Book> Books = em.createNamedQuery("Book.findAll", Book.class).getResultList();
        return Books;
    }
    @Override
    public Book findById(Long id) {
        return em.find(Book.class, id);
    }
    @Override
    public Book enregistrer(Book entity) {
        em.persist(entity);
        return entity;
    }
    @Override
    public void delete(Book entity) {
        em.remove(em.merge(entity));
    }
    @Override
    public Book update(Book entity) {
        return em.merge(entity);
    }
    @Override
    public String getReason() {
        return "why je ne fonctionnne pas?????";
    }
    public void setEm(EntityManager em) {
        this.em = em;
    }   
}

本地界面:

    package cd.espoirmur.Ejb;

import java.util.List;
import javax.ejb.Local;
import javax.persistence.EntityManager;

@Local
public interface InterfaceEjbLocal<T> {

    public List<T> find();

    public T findById(Long id);

    public T enregistrer(T entity);

    public void delete(T entity);

    public T update(T entity);

    public String getReason();

}

我的persistance.xml在这里:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="BookStorePU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:global/jdbc/BookStorePool</jta-data-source>
    <class>cd.espoirmur.Entity.Book</class>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
      <property name="eclipselink.logging.level" value="INFO"/>
    </properties>
  </persistence-unit>
</persistence>

并且pom.xml文件是:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cd.espoirmur</groupId>
    <artifactId>TestEjbContainer</artifactId>
    <version>alpha</version>
    <packaging>war</packaging>

    <name>TestEjbContainer</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <glassfish.embedded-static-shell.jar>C:\Program Files\glassfish-4.1.1\glassfish\lib\embedded\glassfish-embedded-static-shell.jar</glassfish.embedded-static-shell.jar>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
         <dependency>
            <groupId>org.glassfish.main.extras</groupId>
            <artifactId>glassfish-embedded-static-shell</artifactId>
            <version>4.1.1</version>
            <scope>system</scope>
            <systemPath>${glassfish.embedded-static-shell.jar}</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
有人能帮帮我吗?

我认为错误是在我的ejb生命周期中,当我尝试注入持久性单元但我不知道如何解决它时,如果我没有实现本地接口,我得到相同的错误rven .. 请帮帮我!!!!!

1 个答案:

答案 0 :(得分:1)

我可能错了,但看起来问题是容器没有设法设置持久性单元,这是因为您还没有提供所有特定于供应商的必需属性Eclipse持久性提供程序需要(特别是连接凭据)。

这些属于&#34;属性&#34;部分,并且是特定于供应商的(JBoss docs link),但是here's the example Eclipse Link提供了关于此主题的their docs。因此,我建议将其添加到您的persistance.xml中(使用适当的数据库值):

<properties>
    <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
    <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
    <property name="javax.persistence.jdbc.user" value="scott"/>
    <property name="javax.persistence.jdbc.password" value="tiger"/>
</properties>