2 Replies Latest reply: Nov 11, 2010 4:29 AM by Domenico Nappo RSS

    Testing a simple session bean in glassfish embedded container

    Domenico Nappo Newbie

      Hi there,

       

      I've just discovered Arquillana and it seems a great product.

       

      Although the setup is s relatively easier than other test frameworks, I have some difficulties to get it working.

       

      I'm developing a simple (so far) Java EE 6 enteprise application in NetBeans/Maven.

       

      Now, I get an error so I still cannot execute tests. Please, see below source code, configurations and error's details (I'm even new with the ejb3 programming model hence if you notice some inconsistencies, let me know please).

       

      Here the relevant code and configurations. Hope it's enough. Many thanks in advance!

       

      Login Entity source code

       

      package efg.entity;

       

      import java.io.Serializable;
      ...

       

      /**
      *
      */
      @Entity
      @Table(name = "LOGIN", catalog = "", schema = "EFGPRD_METADATA")
      @NamedQueries({
          @NamedQuery(name = "Login.findAll", query = "SELECT l FROM Login l"),
          @NamedQuery(name = "Login.findById", query = "SELECT l FROM Login l WHERE l.id = :id"),
          ...

      })
      public class Login implements Serializable {
          private static final long serialVersionUID = 1L;
          @Id
          @Basic(optional = false)
          @Column(name = "ID")
          private Long id;
          @Column(name = "INSTITUTIONFK")
          private Long institutionfk;
          ...

          ...


          @Column(name = "TOTALVIEWS")
          private Long totalviews;
          @Basic(optional = false)
          @Column(name = "ENABLED")
          private short enabled;

       

          public Login() {
          }

       

          public Login(Long id) {
              this.id = id;
          }

       

          ...

          ...

       

          public Long getTotalviews() {
              return totalviews;
          }

       

          public void setTotalviews(Long totalviews) {
              this.totalviews = totalviews;
          }

       

          ...

          ...

       

          @Override
          public int hashCode() {
              int hash = 0;
              hash += (id != null ? id.hashCode() : 0);
              return hash;
          }

       

          @Override
          public boolean equals(Object object) {
              // TODO: Warning - this method won't work in the case the id fields are not set
              if (!(object instanceof Login)) {
                  return false;
              }
              Login other = (Login) object;
              if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                  return false;
              }
              return true;
          }

       

      }

       

      LoginBean Session Bean source code

       

      package efg.ejb;

       

      import efg.entity.Login;
      import javax.ejb.Stateful;
      ...

       

      /**
      *
      */
      @Stateful(mappedName="ejb/LoginSessionBeanJNDI")
      public class LoginBean implements LoginBeanLocal {
          @PersistenceContext(unitName="EFG_MetadataCatalogue-ejbPU")
          private EntityManager em;
          private Login login;

       

          public EntityManager getEm() {
              return em;

          }

       

          public void setEm(EntityManager em) {
              this.em = em;
          }

       

       

          @Override

          public Boolean logIn(String userName, String encPassword) {
             
              login = null;
              login = (Login)em.createNamedQuery("Login.findByUsernameinsti").setParameter("usernameinsti", userName).getSingleResult();
              if (login!=null) {
                  return Boolean.TRUE;
              } else {
                  return Boolean.FALSE;
              }
          }

       

          @Override
          public Boolean logOut() {
              login = null;
              return Boolean.TRUE;

          }

       

          @Override
          public Login getLogin() {
              return login;
          }

       

      LoginBeanTest source code

       

      package efg.ejbtests;

       

      import efg.ejb.LoginBean;
      import efg.entity.Login;
      import javax.ejb.EJB;
      import junit.framework.Assert;

      ...

      ...

       

      /**
      *
      */
      @RunWith(Arquillian.class)
      public class LoginBeanTest {
         
          @EJB
          private LoginBean loginService;

       

         @Deployment
         public static JavaArchive createTestArchive() {

       

            return ShrinkWrap.create(JavaArchive.class, "testMetadataCatalogueEjbs.jar").addClasses(Login.class, LoginBean.class);

       

         }

       

         @Test
         public void shouldBeAbleToInjectEJB() throws Exception {

        
            Assert.assertNotNull(loginService);

         }

       

         @Test
         public void LoginMethodTest() throws Exception {

       

            String userName = "testadminuser";
            String pass = "admin";
          
            Assert.assertTrue(loginService.logIn(userName, pass));
            Assert.assertNotNull(loginService.getLogin());
            Assert.assertFalse(loginService.logIn("x", "x"));


         }

       

      }

      POM.xml dependencies

       

      <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.8.2</version>
                  <scope>test</scope>
              </dependency>

       

              <dependency>
                  <groupId>org.jboss.arquillian</groupId>
                  <artifactId>arquillian-junit</artifactId>
                  <version>${arquillian.version}</version>
                  <scope>test</scope>
              </dependency>

       

               <dependency>
                  <groupId>org.jboss.arquillian.container</groupId>
                  <artifactId>arquillian-glassfish-embedded-3</artifactId>
                  <version>${arquillian.version}</version>
                  <scope>test</scope>
              </dependency>

       

              <dependency>
                  <groupId>org.glassfish.extras</groupId>
                  <artifactId>glassfish-embedded-all</artifactId>
                  <version>3.0.1-b02</version>       
              </dependency>

          </dependencies>

       

      Stack Trace Exception

       

      Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 10.025 sec <<< FAILURE!
      shouldBeAbleToInjectEJB(efg.ejbtests.LoginBeanTest)  Time elapsed: 0.014 sec  <<< ERROR!
      org.jboss.arquillian.impl.event.FiredEventException: java.lang.IllegalStateException: Error launching test

      efg.ejbtests.LoginBeanTest public void efg.ejbtests.LoginBeanTest.shouldBeAbleToInjectEJB() throws java.lang.Exception
              at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:68)
              at org.jboss.arquillian.impl.context.AbstractEventContext.fire(AbstractEventContext.java:115)
              at org.jboss.arquillian.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:157)
              at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:244)
              at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:207)
              at org.jboss.arquillian.junit.Arquillian$5$1.evaluate(Arquillian.java:225)
              at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
              at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:221)
              at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
              at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:163)
              at org.jboss.arquillian.junit.Arquillian$3$1.evaluate(Arquillian.java:186)
              at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
              at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:182)
              at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
              at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:127)
              at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
              at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
              at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
              at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
              at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
      Caused by: java.lang.IllegalStateException: Error launching test efg.ejbtests.LoginBeanTest public void efg.ejbtests.LoginBeanTest.shouldBeAbleToInjectEJB() throws java.lang.Exception
              at org.jboss.arquillian.protocol.servlet_3.ServletMethodExecutor.invoke(ServletMethodExecutor.java:61)
              at org.jboss.arquillian.impl.handler.ContainerTestExecuter.callback(ContainerTestExecuter.java:50)
              at org.jboss.arquillian.impl.handler.ContainerTestExecuter.callback(ContainerTestExecuter.java:40)
              at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:63)
              ... 31 more
      Caused by: java.net.SocketException: Unexpected end of file from server
              at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:769)
              at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
              at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:766)
              at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
              at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1072)
              at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
              at org.jboss.arquillian.protocol.servlet_3.ServletMethodExecutor.execute(ServletMethodExecutor.java:82)
              at org.jboss.arquillian.protocol.servlet_3.ServletMethodExecutor.invoke(ServletMethodExecutor.java:57)
              ... 34 more

       

      LoginMethodTest(efg.ejbtests.LoginBeanTest)  Time elapsed: 0.011 sec  <<< ERROR!
      org.jboss.arquillian.impl.event.FiredEventException: java.lang.IllegalStateException: Error launching test efg.ejbtests.LoginBeanTest public void efg.ejbtests.LoginBeanTest.LoginMethodTest() throws java.lang.Exception
              at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:68)
              at org.jboss.arquillian.impl.context.AbstractEventContext.fire(AbstractEventContext.java:115)
              at org.jboss.arquillian.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:157)
              at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:244)
              at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:207)
              at org.jboss.arquillian.junit.Arquillian$5$1.evaluate(Arquillian.java:225)
              at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
              at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:221)
              at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
              at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:163)
              at org.jboss.arquillian.junit.Arquillian$3$1.evaluate(Arquillian.java:186)
              at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
              at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:182)
              at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
              at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:127)
              at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
              at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
              at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
              at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
              at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
      Caused by: java.lang.IllegalStateException: Error launching test efg.ejbtests.LoginBeanTest public void efg.ejbtests.LoginBeanTest.LoginMethodTest() throws java.lang.Exception
              at org.jboss.arquillian.protocol.servlet_3.ServletMethodExecutor.invoke(ServletMethodExecutor.java:61)
              at org.jboss.arquillian.impl.handler.ContainerTestExecuter.callback(ContainerTestExecuter.java:50)
              at org.jboss.arquillian.impl.handler.ContainerTestExecuter.callback(ContainerTestExecuter.java:40)
              at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:63)
              ... 31 more
      Caused by: java.net.SocketException: Unexpected end of file from server
              at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:769)
              at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
              at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:766)
              at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
              at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1072)
              at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
              at org.jboss.arquillian.protocol.servlet_3.ServletMethodExecutor.execute(ServletMethodExecutor.java:82)
              at org.jboss.arquillian.protocol.servlet_3.ServletMethodExecutor.invoke(ServletMethodExecutor.java:57)
              ... 34 more

        • 1. Re: Testing a simple session bean in glassfish embedded container
          Fábio Figueiredo Newbie

          I am a begginer as well in Arquillian and I am not sure, but I had a similar error few hours ago when I tried to start Glassfish Embedded while other services were using the same port. Then the same exception as you saw, i.e.,

           

          "java.net.SocketException: Unexpected end of file from server
                  at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:769)
                  at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
                  at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:766)
                  at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)"

           

          was triggered here, since Glassfish Embedded's default port is 8181 and there was another service using this port as well.
          After I stopped this service, my test using Glassfish Embedded could run nicely.

           

          Maybe this could help.

           

          Best Regards,
          Fábio

          • 2. Re: Testing a simple session bean in glassfish embedded container
            Domenico Nappo Newbie

            Many Thanks Fabio,

            Indeed, I was having Glassfish started and Grizzly listens on 8181.

             

            INFO: Grizzly Framework 1.9.18-o started in: 47ms listening on port 4848
            INFO: Grizzly Framework 1.9.18-o started in: 53ms listening on port 8181

             

            Now I've got further problems with injection but I will post a new thread if I can't solve them