title: JUnit Test with EJB 3 and glassfish using java:module scoped resources
body: <p>I am trying to get my JUnit test running which uses stateless EJBs from the same and different project and persistence (using a jta-data-source).</p>
  
  <p>When using the following configuration, Glassfish wants me to use java:module scope instead of java:app scope in the glassfish-resource.xml(s).</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"&gt;
  
  &lt;resources&gt;
      &lt;jdbc-connection-pool name="java:app/jdbc/test_pool"
          res-type="javax.sql.DataSource" datasource-classname="org.postgresql.ds.PGSimpleDataSource"
          pool-resize-quantity="1" max-pool-size="5" steady-pool-size="0"
          statement-timeout-in-seconds="30"&gt;
          &lt;property name="User" value="abc"&gt;&lt;/property&gt;
          &lt;property name="DatabaseName" value="test_db"&gt;&lt;/property&gt;
          &lt;property name="Password" value="abc"&gt;&lt;/property&gt;
          &lt;property name="ServerName" value="192.168.100.1"&gt;&lt;/property&gt;
          &lt;property name="Ssl" value="false"&gt;&lt;/property&gt;
          &lt;property name="TcpKeepAlive" value="false"&gt;&lt;/property&gt;
          &lt;property name="SocketTimeout" value="0"&gt;&lt;/property&gt;
          &lt;property name="PortNumber" value="1234"&gt;&lt;/property&gt;
          &lt;property name="LoginTimeout" value="0"&gt;&lt;/property&gt;
          &lt;property name="PrepareThreshold" value="5"&gt;&lt;/property&gt;
          &lt;property name="UnknownLength" value="2147483647"&gt;&lt;/property&gt;
      &lt;/jdbc-connection-pool&gt;
      &lt;jdbc-resource pool-name="java:app/jdbc/test_pool"
          jndi-name="java:app/jdbc/test"&gt;&lt;/jdbc-resource&gt;
  &lt;/resources&gt;      
  </code></pre>
  
  <p>The Error forcing me to use java:module scope:</p>
  
  <pre><code>INFORMATION: [EJBContainerImpl] Deploying as a File
  Aug 22, 2014 2:15:33 PM org.glassfish.api.ActionReport failure
  SCHWERWIEGEND: Exception while deploying the app [ejb-app]
  Aug 22, 2014 2:15:33 PM com.sun.enterprise.v3.server.ApplicationLifecycle deploy
  SCHWERWIEGEND: Exception during lifecycle processing
  org.glassfish.deployment.common.DeploymentException: Failue while processing glassfish-resources.xml(s) in the archive  -- Resource [ java:app/jdbc/test_pool ] is not allowed to specify the scope [ java:app/ ]. Acceptable scope for this resource is [ java:module/ ]
      at org.glassfish.resources.admin.cli.ResourcesXMLParser.getScopedName(ResourcesXMLParser.java:529)
      at org.glassfish.resources.admin.cli.ResourcesXMLParser.generateJDBCConnectionPoolResource(ResourcesXMLParser.java:708)
      at org.glassfish.resources.admin.cli.ResourcesXMLParser.generateResourceObjects(ResourcesXMLParser.java:334)
      at org.glassfish.resources.admin.cli.ResourcesXMLParser.&lt;init&gt;(ResourcesXMLParser.java:153)
      at org.glassfish.resources.module.ResourcesDeployer.processArchive(ResourcesDeployer.java:274)
      at org.glassfish.resources.module.ResourcesDeployer.processResources(ResourcesDeployer.java:766)
      at org.glassfish.resources.module.ResourcesDeployer.event(ResourcesDeployer.java:715)
      at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:416)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
      at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
      at java.security.AccessController.doPrivileged(Native Method)
      at javax.security.auth.Subject.doAs(Subject.java:356)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
      at com.sun.enterprise.admin.cli.embeddable.DeployerImpl.deploy(DeployerImpl.java:133)
      at org.glassfish.ejb.embedded.EJBContainerImpl.deploy(EJBContainerImpl.java:141)
      at org.glassfish.ejb.embedded.EJBContainerProviderImpl.createEJBContainer(EJBContainerProviderImpl.java:134)
      at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:127)
      at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:102)
      at de.prototype.impl.prototype.crud.TestJavaEE.setUpClass(TestJavaEE.java:38)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  
  Aug 22, 2014 2:15:33 PM org.glassfish.deployment.admin.DeployCommand execute
  </code></pre>
  
  <p>After changing the scope to java:modue:</p>
  
  <pre><code>...
  &lt;jdbc-connection-pool name="java:module/jdbc/test_pool"
  ...
      &lt;jdbc-resource pool-name="java:module/jdbc/test_pool"
          jndi-name="java:module/jdbc/test"&gt;&lt;/jdbc-resource&gt;
  ...
  </code></pre>
  
  <p>I recieve the following Exception(s):</p>
  
  <pre><code>Aug 22, 2014 2:22:35 PM org.glassfish.api.ActionReport failure
  SCHWERWIEGEND: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
  Aug 22, 2014 2:22:35 PM org.glassfish.api.ActionReport failure
  SCHWERWIEGEND: Exception while invoking class org.glassfish.javaee.full.deployment.EarDeployer prepare method
  org.glassfish.deployment.common.DeploymentException: Invalid resource : java:module/jdbc/test__pm
      at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:180)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:922)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:431)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
      at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
      at java.security.AccessController.doPrivileged(Native Method)
      at javax.security.auth.Subject.doAs(Subject.java:356)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
      at com.sun.enterprise.admin.cli.embeddable.DeployerImpl.deploy(DeployerImpl.java:133)
      at org.glassfish.ejb.embedded.EJBContainerImpl.deploy(EJBContainerImpl.java:141)
      at org.glassfish.ejb.embedded.EJBContainerProviderImpl.createEJBContainer(EJBContainerProviderImpl.java:134)
      at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:127)
      at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:102)
      at de.prototype.impl.prototype.crud.TestJavaEE.setUpClass(TestJavaEE.java:38)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  Caused by: java.lang.RuntimeException: Invalid resource : java:module/jdbc/test__pm
      at com.sun.enterprise.connectors.ConnectorRuntime.lookupDataSourceInDAS(ConnectorRuntime.java:593)
      at com.sun.enterprise.connectors.ConnectorRuntime.lookupPMResource(ConnectorRuntime.java:517)
      at org.glassfish.persistence.common.PersistenceHelper.lookupPMResource(PersistenceHelper.java:63)
      at org.glassfish.persistence.jpa.ProviderContainerContractInfoBase.lookupDataSource(ProviderContainerContractInfoBase.java:71)
      at org.glassfish.persistence.jpa.PersistenceUnitInfoImpl.&lt;init&gt;(PersistenceUnitInfoImpl.java:108)
      at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:142)
      at org.glassfish.persistence.jpa.PersistenceUnitLoader.&lt;init&gt;(PersistenceUnitLoader.java:107)
      at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
      at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
      at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
      at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:922)
      at org.glassfish.javaee.full.deployment.EarDeployer.prepareBundle(EarDeployer.java:307)
      at org.glassfish.javaee.full.deployment.EarDeployer.access$200(EarDeployer.java:88)
      at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:153)
      at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:150)
      at org.glassfish.javaee.full.deployment.EarDeployer.doOnBundles(EarDeployer.java:230)
      at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllTypedBundles(EarDeployer.java:239)
      at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllBundles(EarDeployer.java:265)
      at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:150)
      ... 36 more
  Caused by: com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : java:module/jdbc/test__pm
      at org.glassfish.jdbcruntime.service.JdbcDataSource.validateResource(JdbcDataSource.java:81)
      at org.glassfish.jdbcruntime.service.JdbcDataSource.setResourceInfo(JdbcDataSource.java:62)
      at org.glassfish.jdbcruntime.JdbcRuntimeExtension.lookupDataSourceInDAS(JdbcRuntimeExtension.java:136)
      at com.sun.enterprise.connectors.ConnectorRuntime.lookupDataSourceInDAS(ConnectorRuntime.java:589)
      ... 55 more
  </code></pre>
  
  <p>Here is also the persistence.xml using the glassfish resource:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"
      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_2_0.xsd"&gt;
      &lt;persistence-unit name="test_pu" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;java:module/jdbc/test&lt;/jta-data-source&gt;
          &lt;properties&gt;
              &lt;property name="eclipselink.ddl-generation" value="create-tables" /&gt;
              &lt;property name="eclipselink.logging.level" value="FINE" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I found that the module scope on client side is different then the app scope (if i understand it right):
  <a href="https://blogs.oracle.com/JagadishPrasath/entry/applying_scope_for_jdbc_resources" rel="nofollow">https://blogs.oracle.com/JagadishPrasath/entry/applying_scope_for_jdbc_resources</a></p>
  
  <p><strong>But how is it possible to use a JUnit test with persisting, using the glassfish-resource.xml???</strong></p>
  
  <p>If i comment the persistence-unit block out, the JUnit tests which are not persisting are working. I also can not use java:app scope, because then glassfish wants me to use java:module scope.</p>
  
  <p>Here is the rest of the code (the "testAddPerson()" JUnit test will not work with persistence):</p>
  
  <pre><code>public class TestJavaEE {
  
      private static EJBContainer ejbContainer;
      private static Context ctx;
  
      @BeforeClass
      public static void setUpClass() throws NamingException {
          ejbContainer = EJBContainer.createEJBContainer();
          System.out.println("Starting the container");
          ctx = ejbContainer.getContext();
      }
  
      @AfterClass
      public static void tearDownClass() throws NamingException {
          ejbContainer.close();
          System.out.println("Closing the container");
      }
  
      protected Object lookup(String name) throws NamingException {
          return ctx.lookup(name);
      }
  }
  
  public class PersonLogicBean2Test extends TestJavaEE {
  
      @EJB
      private PersonLogic2 personLogic2;
  
      @Before
      public void setUp() throws Exception {
          //personLogic2 = (PersonLogic2) lookup("java:global/classes/PersonLogicBean2");
          personLogic2 = (PersonLogic2) lookup("java:global/ejb-app/classesejb/PersonLogicBean2");
      }
  
      @Test
      public void testGetPersonInfo() throws NamingException {
          String result = personLogic2.getPersonInfo("xx");
          System.out.println("Result: " + result);
          Assert.assertEquals("PersonLogicBean2xx", result);
      }
  
      @Test
      public void testAddPerson() throws NamingException {
          int cnt = personLogic2.getPersonCount();
          System.out.println("Count before: " + cnt);
          personLogic2.addPerson("jo");
          int result = personLogic2.getPersonCount();
          System.out.println("Count after: " + result);
          Assert.assertEquals(cnt + 1, result);
      }
  
      @Test
      public void testAdd() throws NamingException {
          Assert.assertEquals(5, personLogic2.add(2, 3));
      }
  }
  
  @Stateless(mappedName="PersonLogicBean2")
  public class PersonLogicBean2 implements PersonLogic2 {
  
      private static final Logger logger = LoggerFactory.getLogger(PersonLogicBean2.class);
  
      @EJB
      private EmployeeCrudBean emplyeeCrud;
  
      @EJB
      private CalculatorBeanRemote calculatorBean;
  
      @Override
      public String toString() {
          return "it works";
      }
  
      @Override
      public int getPersonCount() {
          return emplyeeCrud.getAllEmployees().size();
      }
  
      @Override
      public void addPerson(String name) {
          emplyeeCrud.addEmployee(new String[] { name });
      }
  
      @Override
      public String getPersonInfo(String person) {
          logger.info("Called getPersonInfo {}", person);
          return "PersonLogicBean2" + person;
      }
  
      @Override
      public int add(int a, int b) {
          //return a + b;
          return calculatorBean.add(a, b);
      }
  
      @Override
      public int subtract(int a, int b) {
          //return a - b;
          return calculatorBean.add(a, b);
      }
  }
  
  @Stateless(mappedName="EmployeeCrudBean")
  public class EmployeeCrudBean {
  
      @PersistenceContext
      private EntityManager em;
  
      public void addEmployee(String[] names) {
          for (String name : names) {
              Employee e = new Employee();
              e.setName(name);
              em.persist(e);
          }
      }
  
      public List&lt;Employee&gt; getAllEmployees() {
          CriteriaBuilder cb = em.getCriteriaBuilder();
          CriteriaQuery&lt;Employee&gt; query = cb.createQuery(Employee.class);
          Root&lt;Employee&gt; root = query.from(Employee.class);
  
          query.select(root);
          query.orderBy(cb.asc(root.get("id")));
  
          List&lt;Employee&gt; ret = em.createQuery(query).getResultList();
          return ret;
      }
  }
  </code></pre>