1 2 Previous Next 22 Replies Latest reply on Apr 17, 2011 6:01 AM by Aslak Knutsen

    Simple example - no luck for a week!

    Rune Molin Newbie

      Hello everyone,

       

      Could anybody please point out what it is I am missing in this very simple example of unit testing with arquillian. I've been on this for the best part of a week now!

       

      This is my unit test:

       

       

      {code}

      @RunWith(Arquillian.class)

      public class Tests {

       

          @Inject

          TestServiceInterface service;

       

          @Deployment

          public static Archive<?> createDeployment() {

              WebArchive archive = ShrinkWrap.create(WebArchive.class, "test.war")

                      .addClasses(TestService.class, TestServiceInterface.class)

                      .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

       

              return archive;

          }

       

          @Test

          public void test() throws Exception {

              String result = service.say("Hello");

              System.out.println(result);

          }

      }

      {code}

       

       

      The service:

       

      {code}

      @Stateless

      @Local(TestServiceInterface.class)

      public class TestService implements TestServiceInterface{

       

          @Override

          public String say(String s) {

              return '"' + s + '"';

          }

      }

      {code}

       

      The interface:

       

      {code}

      public interface TestServiceInterface {

          String say(String s);

      }

      {code}

       

      The POM

       

      {code}

      <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>

       

          ...

       

          <packaging>war</packaging>

       

          ...

       

          <properties>

              <arquillian.version>1.0.0.Alpha5</arquillian.version>

          </properties>

       

          <dependencies>

               <dependency>

                  <groupId>org.jboss.spec</groupId>

                  <artifactId>jboss-javaee-6.0</artifactId>

                  <type>pom</type>

              </dependency>

       

              <dependency>

                  <groupId>org.jboss.arquillian</groupId>

                  <artifactId>arquillian-junit</artifactId>

                  <version>${arquillian.version}</version>

                  <scope>test</scope>

              </dependency>

          </dependencies>

       

          <profiles>

       

              <profile>

                  <id>arquillian-weld-ee-embedded-1.1</id>

                  <activation>

                      <activeByDefault>true</activeByDefault>

                  </activation>

                  <dependencies>

                      <dependency>

                          <groupId>org.jboss.arquillian.container</groupId>

                          <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>

                          <version>${arquillian.version}</version>

                          <scope>test</scope>

                      </dependency>

                      <dependency>

                          <groupId>org.jboss.weld</groupId>

                          <artifactId>weld-core</artifactId>

                          <scope>test</scope>

                      </dependency>

                      <dependency>

                          <groupId>org.jboss.weld</groupId>

                          <artifactId>weld-api</artifactId>

                          <scope>test</scope>

                      </dependency>

                      <dependency>

                          <groupId>org.slf4j</groupId>

                          <artifactId>slf4j-simple</artifactId>

                          <scope>test</scope>

                      </dependency>

                  </dependencies>

       

                  <dependencyManagement>

                      <dependencies>

                          <dependency>

                              <groupId>org.jboss.weld</groupId>

                              <artifactId>weld-core-bom</artifactId>

                              <version>1.1.0.Final</version>

                              <type>pom</type>

                              <scope>import</scope>

                          </dependency>

                      </dependencies>

                  </dependencyManagement>

              </profile>

          </profiles>

       

      </project>

      {code}

       

      And this is the error I get when I run test using the arquillian-weld-ee-embedded-1.1 profile:

       

      {code}

      java.lang.NullPointerException

          at org.jboss.weld.util.reflection.SecureReflections.lookupMethod(SecureReflections.java:432)

          at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:123)

          at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:62)

          at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:125)

          at dk.cph.cditest.org$jboss$weld$bean-test$war-SessionBean-TestService_$$_WeldProxy.say(org$jboss$weld$bean-test$war-SessionBean-TestService_$$_WeldProxy.java)

          at dk.cph.cditest.Tests.test(Tests.java:31)

      {code}

       

      I also have a very simple webservice facade in the same war where TestServiceInterface is @injected. That runs with no problems when deployed to JBoss 6 Final

       

      I also tried to ruhn with the Jboss 6 embedded container, but I gave up on that for the time being, due to differences in versions of Shrinkwrap and all kinds of weird Maven behaviour.

       

      Any help and input much appreciated

       

      Rune Molin

        • 1. Re: Simple example - no luck for a week!
          Mark Temple Newbie

           

          I feel your pain!

          http://community.jboss.org/thread/164548

           

          Would be curious if the @inject is failing or not...

          In your Tests.java, replace:

           

           

          @Test

          public void test() throws Exception {

              String result = service.say("Hello");

              System.out.println(result);

          }

           

          With:

           

          @Test

          public void test() {

              Assert.assertNotNull(service);

          }

           

          Let's see if what that does...

           

          Mark

          • 2. Simple example - no luck for a week!
            Rune Molin Newbie

            Nope, same result. The assertion holds true and the service is injected, it has the class "dk.cph.cditest.org$jboss$weld$bean-test$war-SessionBean-TestService_$$_WeldProxy" but any attempt to call methods on the service fails with the same NullPointerException.

             

            Also tried turning the bean into a no-interface view, with the same saddening outcome

             

            /Rune

            • 3. Simple example - no luck for a week!
              Mark Temple Newbie

              I wish I could get to the point you are at !

               

              I can't even get it to inject... (Jboss 6.0 remote, Eclipse Helios/No Maven, Arquillian Alpha5, JUnit)

               

              Man, you've only been trying this for a week?  Try 2 weeks!

               

              Mark

              • 4. Simple example - no luck for a week!
                Aslak Knutsen Master

                The problem is your trying to run a EJB in the Weld EE Mock environment. Weld EE can wrap/proxy EJBs etc, but not actually run them, then it needs the EJBContainer. It fails with a NullPointerException because the EJBContainer integration layer is mocked and always returns null on lookup.

                 

                If you use the Weld SE container, this example should run (if you change the deployment type to a JavaArchive, SE does not understand WebArchives). Tho all annotations etc on your classes are ignored, so what you are injecting is not a SessionBean, but rather a CDI bean.

                 

                -aslak-

                1 of 1 people found this helpful
                • 5. Simple example - no luck for a week!
                  Rune Molin Newbie

                  How un-intuitive is that ?

                   

                  Now I get a ClassNotFound on org.jboss.weld.context.api.BeanStore. I added "weld-spi" as a dependency in addition to the weld-se-embedded-11 profile from the reference, but it won't run

                  • 6. Simple example - no luck for a week!
                    Aslak Knutsen Master

                    You need to use the org.jboss.arquillian.container:arquillian-weld-se-embedded-1.1 container if you run against Weld 1.1.0.Final

                    • 7. Simple example - no luck for a week!
                      Aslak Knutsen Master

                      I agree it might be a bit confusing, but it's really not as un-intuitive as it sounds.

                       

                      Weld can inject EE resources in a EE environment, but Weld alone is only a CDI Bean Container. Weld EE Mock Arquillian Container only starts a Weld Container with some added background mocking and mimicks some of the Scopes(Request, Session, Conversation) that is found in the EE environment. But it can't create the EE resources like EJB Beans, JNDI or PersistenceContexts, that is outside it's scope. The Weld EE Mock container is used to test CDI Beans behavior simulating how it would be in a EE environment.

                       

                      When you run it in the Weld SE Arquillian Container, it starts a 'pure' Weld Bean container, without any mocking or EE Scopes activated. The reason why your 'EJB SessionBean' runs here is based on how CDI does Bean resolving. Weld does not understand @Local or @Statless, so those are ignored. But if you @Inject a interface, and there is only one implementation of that interface, it can create a CDI Bean. So basically what you are getting is a @Inject MyInterface = new MyInterfaceImpl();. No @EJB/@Resource injection support, no @TransactionAttributes, no security etc, which are all things that are handled by a EJB container.

                       

                      In the same way, CDI Beans are not understood by the OpenEJB container.

                       

                      These small standalone Embedded Containers are ment to test individual technologies/specifications, like CDI or EJB. If you want to combine them, e.g. @Inject EJBs in CDI Beans, you need a Container/Environment that supports all of these technologies/specifications, e.g. EE6.

                      So to get the full support of EE6, you need to use e.g. JBoss AS or GlassFish.

                       

                      -aslak-

                      • 8. Simple example - no luck for a week!
                        Rune Molin Newbie

                        Well, I copied the configuration for weld-se-embedded-11 straight from the reference and that refers to "arquillian-weld-se-embedded-1", so perhaps that need to be updated.

                         

                        But it actually works now - phew!

                        • 10. Simple example - no luck for a week!
                          Richard Yang Apprentice

                          I am also struggling with alpha5. I configured arquillian.xml to point to remote jboss6 server. And I also include jndi.properties which has correct java.naming.provider.url. However, it seems that the test only works if the remote jboss server binds to 0.0.0.0. If I bind the remote server to a specific ip address, then it would say 127.0.0.1 is refused and test would never run. Very strange. Jboss does not recommend to bind to 0.0.0.0, why we are forced to do so here. Also, our linux server which hosts jboss AS has multiple NICs so binding to 0.0.0.0 is not possible.

                           

                          My arquillian.xml is as follows:

                          <?xml version="1.0"?>

                           

                          <arquillian xmlns="http://jboss.com/arquillian"

                           

                                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                           

                                  xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

                           

                           

                           

                          <container qualifier="jbossas" default="true">

                           

                               <configuration>

                           

                                  <property name="providerUrl">jnp://myRemoteAS:1099</property>

                           

                               </configuration>

                           

                               <protocol type="Servlet 3.0">

                           

                                  <configuration>

                           

                                      <property name="host">myRemoteAS</property>

                           

                                      <property name="port">8080</property>

                           

                                  </configuration>

                           

                               </protocol>

                           

                          </container>

                           

                          </arquillian>

                          • 11. Simple example - no luck for a week!
                            Aslak Knutsen Master

                            jndi.properties are no longer used when connecting to the container.

                             

                            Could you provide the exception your getting?

                            • 12. Simple example - no luck for a week!
                              Richard Yang Apprentice

                              Thanks for responding. I tried on my jboss6 Final on my local machine but instead of binding to 0.0.0.0, I bind the jboss server to the specific IP address. Also I removed the jndi.properties as you suggested. Now I got the following error ( I ran the test inside eclipse) from the junit console:

                               

                              org.jboss.arquillian.spi.client.container.LifecycleException: Could not connect to container

                                  at org.jboss.arquillian.container.jbossas.remote_6.JBossASRemoteContainer.start(JBossASRemoteContainer.java:83)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController$5.perform(ContainerLifecycleController.java:145)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController$5.perform(ContainerLifecycleController.java:135)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController.forContainer(ContainerLifecycleController.java:183)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController.startContainer(ContainerLifecycleController.java:134)

                                  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.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.invokeObservers(EventContextImpl.java:98)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:80)

                                  at org.jboss.arquillian.impl.client.ContainerDeploymentContextHandler.createContainerContext(ContainerDeploymentContextHandler.java:78)

                                  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.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:87)

                                  at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:126)

                                  at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:106)

                                  at org.jboss.arquillian.impl.core.EventImpl.fire(EventImpl.java:67)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController$2.perform(ContainerLifecycleController.java:83)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController$2.perform(ContainerLifecycleController.java:76)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController.forEachContainer(ContainerLifecycleController.java:176)

                                  at org.jboss.arquillian.impl.client.container.ContainerLifecycleController.startContainers(ContainerLifecycleController.java:75)

                                  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.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.invokeObservers(EventContextImpl.java:98)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:80)

                                  at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:126)

                                  at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:106)

                                  at org.jboss.arquillian.impl.core.EventImpl.fire(EventImpl.java:67)

                                  at org.jboss.arquillian.impl.client.ContainerEventController.execute(ContainerEventController.java:55)

                                  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.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.invokeObservers(EventContextImpl.java:98)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:80)

                                  at org.jboss.arquillian.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:54)

                                  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.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

                                  at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:87)

                                  at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:126)

                                  at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:106)

                                  at org.jboss.arquillian.impl.EventTestRunnerAdaptor.beforeSuite(EventTestRunnerAdaptor.java:58)

                                  at org.jboss.arquillian.junit.Arquillian.<init>(Arquillian.java:86)

                                  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                                  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

                                  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

                                  at java.lang.reflect.Constructor.newInstance(Unknown Source)

                                  at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)

                                  at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)

                                  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)

                                  at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)

                                  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)

                                  at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)

                                  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)

                                  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)

                                  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)

                                  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)

                                  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)

                                  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

                                  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

                                  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

                              Caused by: javax.naming.CommunicationException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:

                                  java.net.ConnectException: Connection refused: connect]

                                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:841)

                                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688)

                                  at javax.naming.InitialContext.lookup(Unknown Source)

                                  at org.jboss.arquillian.container.jbossas.remote_6.JBossASRemoteContainer.initDeploymentManager(JBossASRemoteContainer.java:197)

                                  at org.jboss.arquillian.container.jbossas.remote_6.JBossASRemoteContainer.start(JBossASRemoteContainer.java:79)

                                  ... 72 more

                              Caused by: java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:

                                  java.net.ConnectException: Connection refused: connect

                                  at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)

                                  at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)

                                  at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)

                                  at sun.rmi.server.UnicastRef.invoke(Unknown Source)

                                  at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)

                                  at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728)

                                  ... 76 more

                              Caused by: java.net.ConnectException: Connection refused: connect

                                  at java.net.PlainSocketImpl.socketConnect(Native Method)

                                  at java.net.PlainSocketImpl.doConnect(Unknown Source)

                                  at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

                                  at java.net.PlainSocketImpl.connect(Unknown Source)

                                  at java.net.SocksSocketImpl.connect(Unknown Source)

                                  at java.net.Socket.connect(Unknown Source)

                                  at java.net.Socket.connect(Unknown Source)

                                  at java.net.Socket.<init>(Unknown Source)

                                  at java.net.Socket.<init>(Unknown Source)

                                  at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)

                                  at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)

                                  ... 82 more

                              • 13. Simple example - no luck for a week!
                                Richard Yang Apprentice

                                Are you sure jndi.properties is not needed anymore? I got different error if I have jndi.properties included. I think the reason why binding to 0.0.0.0 works was because binding to all also includes binding to 127.0.01 or localhost. And somehow, arquillian onlys looks for localhost for jboss remote server even the arquillian.xml tells it to look for a remote server. So I wonder my arquillian.xml is being read or not. I also wonder if my arquillian.xml is correct or if I put it on the right place. Do you know where the arquiliian.xml should be put on the test source or resource folder in maven? The latest Arquillian doc says to put it on the root. But what is the root in the source of maven (I am not talking about the maven target directory).

                                 

                                Thanks.

                                • 14. Simple example - no luck for a week!
                                  Aslak Knutsen Master

                                  arquillian.xml should be in the root of the classpath, so default that would be either src/main/resources or src/test/resources

                                  1 2 Previous Next