title: Testing an EJB with JUnit
body: <p>How should I test an EJB 3.1 which gets an instance of EntityManager injected?</p>
  
  <p>A possible EJB:</p>
  
  <pre><code>@Stateless
  @LocalBean
  public class CommentService {
  
      @PersistenceContext
      private EntityManager em;
  
      public List&lt;Comment&gt; findAll() {
          TypedQuery&lt;Comment&gt; query = em.createNamedQuery(
              Comment.FIND_ALL, Comment.class
          );
          return query.getResultList();
      }
  
  }
  </code></pre>
  
  <p>A possible test:</p>
  
  <pre><code>@Test
  public void testFindAll() {
      List&lt;Comment&gt; all = service.findAll();
      Assert.assertEquals(8, all.size());
  }
  </code></pre>
  
  <p>I am only using GlassFish 3.1 and Eclipse Indigo for Java EE Developers. I already tried things like that:</p>
  
  <pre><code>@Before
  public void setUp() throws Exception {
      ejbContainer = EJBContainer.createEJBContainer();
      service = (CommentService) ejbContainer.getContext()
          .lookup("java:global/classes/CommentService");
  }
  </code></pre>
  
  <p>But all I got was:</p>
  
  <pre><code>javax.ejb.EJBException:
  No EJBContainer provider available: no provider names had been found.
  </code></pre>