title: JBoss Embedded jUnit testing for EJB : NameNotFoundException
body: <p>I'm newbie in EJB and jUnit, and I'm trying to do Embedded testing for the simple EJB-project that running by jBoss AS 7.1.1.Final.</p>
  
  <p>I've written this test:</p>
  
  <pre><code>package com.staff.test.logic;
  
  import java.io.File;
  
  import javax.ejb.embeddable.EJBContainer;
  import javax.naming.Context;
  import javax.naming.NamingException;
  import org.jboss.as.embedded.EmbeddedServerFactory;
  import org.jboss.as.embedded.StandaloneServer;
  import org.junit.Before;
  import org.junit.Test;
  import com.staff.main.logic.ProjectBean;
  
  public class ProjectBeanTest {
  
      private StandaloneServer    server;
      private static EJBContainer ec;
      private static Context ctx;
  
      @Before
      public void initContainer() throws Exception {
      String jbossHomeDir = System.getenv("JBOSS_HOME");
      System.setProperty("jboss.home","C:/eclipse/jboss-as-7.1.1.Final");
      assert jbossHomeDir != null;
      server = EmbeddedServerFactory.create(new File(jbossHomeDir), System.getProperties(), System.getenv(), "org.jboss.logmanager");
      server.start();
  
      ctx=server.getContext();
      }
  
  //    @After
  //    public static void closeContainer() throws Exception {
  //        if (ec != null) {
  //            ec.close();
  //        }
  //    }
  
      @Test
      public void test() throws NamingException {
          ProjectBean bean = (ProjectBean) ctx.lookup("java:global/ProjectBean");
      }
  }
  </code></pre>
  
  <p>But string </p>
  
  <blockquote>
    <p>ProjectBean bean = (ProjectBean) ctx.lookup("java:global/ProjectBean") make exception:</p>
  </blockquote>
  
  <p>do this exception:</p>
  
  <pre><code>javax.naming.NameNotFoundException: ProjectBean -- service jboss.naming.context.java.global.ProjectBean
  at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBa sedNamingStore.java:97)
  at org.jboss.as.naming.NamingContext.lookup(NamingContext.java: 178)
  at org.jboss.as.naming.InitialContext.lookup(InitialContext.jav a:123)
  at org.jboss.as.naming.NamingContext.lookup(NamingContext.java: 214)
  at javax.naming.InitialContext.lookup(Unknown Source)
  at com.staff.test.logic.ProjectBeanTest.test(ProjectBeanTest.ja va:80)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall( FrameworkMethod.java:47)
  at org.junit.internal.runners.model.ReflectiveCallable.run(Refl ectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(Fr ameworkMethod.java:44)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate( InvokeMethod.java:17)
  at org.junit.internal.runners.statements.RunBefores.evaluate(Ru nBefores.java:26)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271 )
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit 4ClassRunner.java:70)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit 4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java: 63)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java :236)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java: 53)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java: 229)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.r un(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(Test Execution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:390)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:197)
  </code></pre>
  
  <p>I dont understand why this string do exception, because I'm newbie.</p>
  
  <p>My project have a bean "ProjectBean" with interface "ProjectBeanLocal" and an entity "Post". And I have a file persistence.xml to work with Oracle 10g.</p>
  
  <p>ProjectBean:</p>
  
  <pre><code>package com.staff.main.logic;
  
  import javax.ejb.LocalBean;
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.hibernate.ejb.Ejb3Configuration;
  import org.hibernate.tool.hbm2ddl.SchemaExport;
  
  import com.staff.main.domain.Post;
  
  /**
   * Session Bean implementation class ProjectBean
   */
  @Stateless
  @LocalBean
  public class ProjectBean implements ProjectBeanLocal {
  
      @PersistenceContext(unitName="StaffPU")
      EntityManager manager;
  
      public ProjectBean() 
      {  
          Ejb3Configuration cfg = new Ejb3Configuration();
          cfg.configure("StaffPU", null);
          SchemaExport schemaExport = new SchemaExport(cfg.getHibernateConfiguration());
          //schemaExport.setOutputFile("schema.sql");
          schemaExport.create(true, true);
      }
  
      @Override
      public void savePost(Post post){
          manager.persist(post);
      }
  
      @Override
      public Post findPost(long id) {
          Post post=manager.find(Post.class, id);
          return post;
      }
  }
  </code></pre>
  
  <p>Interface ProjectBeanLocal:</p>
  
  <pre><code>package com.staff.main.logic;
  
  import javax.ejb.Local;
  import com.staff.main.domain.Post;
  
  @Local
  public interface ProjectBeanLocal {
      void savePost(Post post);
      Post findPost(long id);
  }
  </code></pre>
  
  <p>Entity "Post":</p>
  
  <pre><code>package com.staff.main.domain;
  
  import java.io.Serializable;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.SequenceGenerator;
  
  @Entity
  public class Post implements Serializable{
  
      private static final long serialVersionUID = 6767319776206583629L;
  
      @Id
      @SequenceGenerator(name="ent2seq",sequenceName="seq_post")
      @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="ent2seq")
      private long id;
  
      @Column(nullable=false)
      private String name;
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>And persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence 
          xmlns="http://java.sun.com/xml/ns/persistence"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
          version="1.0"&gt;
      &lt;persistence-unit name="StaffPU"&gt;
          &lt;jta-data-source&gt;java:jboss/datasources/OracleDS&lt;/jta-data-source&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="create" /&gt;
  &lt;!--            &lt;property name="javax.persistence.jdbc.show_sql" value="true" /&gt; --&gt;
  &lt;!--            &lt;property name="hibernate.show_sql" value="true" /&gt; --&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>