13 Replies Latest reply: Mar 29, 2011 7:40 AM by Jelle de Bruin RSS

CDI problem using Arquillian with Tomcat 6.0 and JSFUnit

Jelle de Bruin Newbie

Hi guys,

 

I'm having a problem while using CDI with Arquillian, Tomcat 6.0 and JSFUnit. I think I am almost there but it is constantly saying that the JSFServerSession cannot be resolved:

 

org.jboss.arquillian.spi.ArquillianProxyException: org.jboss.weld.exceptions.UnsatisfiedResolutionException : WELD-001308 Unable to resolve managed beans for Types: [class org.jboss.jsfunit.jsfsession.JSFServerSession]; Bindings: [@javax.enterprise.inject.Default()] [Proxied because : Could not find suitable constructor]

    at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:807)

    at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:793)

 

My test looks like:

 

@RunWith(Arquillian.class)

public class ArquillianTest {

 

    @Deployment

    public static WebArchive createDeployment() {

        System.setProperty("java.io.tmpdir", "c:\\data\\temp\\");

        System.setProperty("maven.repo.local", "c:\\repository");

 

        return ShrinkWrap.create(WebArchive.class, "TestProject.war")

                .setWebXML(new File("src/main/webapp/WEB-INF/web.xml"))

                .addPackage(Package.getPackage("test.example"))

                .addClasses(BackingBean.class)

                .addAsLibraries(MavenArtifactResolver.resolveQualifiedIds("org.jboss.jsfunit:jboss-jsfunit-core:2.0.0.Beta1"))

                .addAsResource(new File("src/main/webapp/views", "test.xhtml"))

                .addAsWebResource(new ByteArrayAsset("<beans/>".getBytes()), "beans.xml")

                .addAsManifestResource(new ByteArrayAsset("<beans/>".getBytes()), ArchivePaths.create("beans.xml"))

                .addAsManifestResource(new File("src/main/webapp/META-INF/context.xml"))

                .addAsWebResource(new File("src/main/webapp/WEB-INF/faces-config.xml"), "faces-config.xml");

    }

 

    @Test

    @InitialPage("/test.xhtml")

    public void testInitialPage(JSFServerSession server) throws IOException {

        Assert.assertEquals("request", server.getManagedBeanValue("#{requestBean.scope}"));

    }

}

 

I have set the context.xml for tomcat:

 

<?xml version="1.0" encoding="UTF-8"?>

<Context>

  <!-- disable storage of sessions across restarts -->

  <Manager pathname="" />

  <Resource name="BeanManager" auth="Container"

    type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" />

  <!-- Uncomment to enable injection into Servlets, Servlet Listeners and Filters in Tomcat -->

  <Listener className="org.jboss.weld.environment.tomcat.WeldLifecycleListener"/>

</Context>

 

And added the things to web.xml:

 

  <listener>

    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>

  </listener>

 

  <resource-env-ref>

    <resource-env-ref-name>BeanManager</resource-env-ref-name>

    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>

  </resource-env-ref>

 

My arquillian.xml file contains:

 

  <tomcat6:container>

    <!-- unpackArchive must be true if using the Weld Servlet module -->

    <tomcat6:unpackArchive>true</tomcat6:unpackArchive>

  </tomcat6:container>

 

And my maven dependencies are:

 

<!-- Test -->

    <dependency>

      <groupId>org.jboss.jsfunit</groupId>

      <artifactId>jsfunit-arquillian</artifactId>

      <version>2.0.0.Beta1</version>

      <scope>test</scope>

    </dependency>

 

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.8.1</version>

      <scope>test</scope>

    </dependency>

 

    <dependency>

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

      <artifactId>arquillian-junit</artifactId>

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

      <scope>test</scope>

    </dependency>

 

    <!-- Container specific dependencies -->

    <dependency>

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

      <artifactId>arquillian-tomcat-embedded-6</artifactId>

      <version>1.0.0.Alpha5</version>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>org.apache.tomcat</groupId>

      <artifactId>catalina</artifactId>

      <version>6.0.29</version>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>org.apache.tomcat</groupId>

      <artifactId>coyote</artifactId>

      <version>6.0.29</version>

      <scope>provided</scope>

    </dependency>

    <dependency>

      <groupId>org.apache.tomcat</groupId>

      <artifactId>jasper</artifactId>

      <version>6.0.29</version>

      <scope>provided</scope>

    </dependency>

    <!-- Weld servlet, EL and JSP required for testing CDI injections -->

    <dependency>

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

      <artifactId>weld-servlet</artifactId>

      <version>1.0.1-Final</version>

      <scope>test</scope>

    </dependency>

    <dependency>

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

      <artifactId>weld-tomcat-support</artifactId>

      <version>1.0.1-CR2</version>

    </dependency>

    <dependency>

      <groupId>org.glassfish.web</groupId>

      <artifactId>el-impl</artifactId>

      <version>2.2</version>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>javax.servlet.jsp</groupId>

      <artifactId>jsp-api</artifactId>

      <version>2.2</version>

      <scope>test</scope>

    </dependency>

 

So what did I do wrong? Maybe it is a classpath issue? Hope somebody can help me.

 

Kind regards,

 

Jelle

  • 1. CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Aslak Knutsen Master

    JSFUnit 2.0.0.Beta1 is not competable with Arquillian 1.0.0.Alpha5. You'll have to downgrade to Arquillian 1.0.0.Alpha4 or wait for JSFUnit 2.0.0.Beta2

     

     

  • 2. CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Jelle de Bruin Newbie

    Thanks Aslak, that indeed seems to be the problem. Now I indeed get a little bit further.

     

    Now my @InitialPage says that it can't find the page:

     

    org.jboss.arquillian.impl.event.FiredEventException: java.lang.IllegalStateException: Error launching test test.example.ArquillianTest public void test.example.ArquillianTest.testInitialPage(org.jboss.jsfunit.jsfsession.JSFServerSession,org.jboss.jsfunit.jsfsession.JSFClientSession) throws java.io.IOException

    ...

    Error launching test at http://localhost:8080/test/ArquillianServletRunner?outputMode=serializedObject&className=test.example.ArquillianTest&methodName=testInitialPage. Got 400 (Bad Request)

     

    I thought that I referenced the page in a correct way?

  • 3. CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Aslak Knutsen Master

    In Alpha4 you have to name your Archive test.war:

     

    return ShrinkWrap.create(WebArchive.class, "test.war")

  • 4. CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Jelle de Bruin Newbie

    Thanks again! That was really weird but it worked

     

    Now my JsfServerSession and JsfClientSession are still NULL. Don't know how that is possible

  • 5. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Aslak Knutsen Master

    It's because in Alpha4 we didn't introspect the deployment, so to be able to call the deployment incontianer, it was hardcoded to call ip:port/test/ArquillianServlet. In Alpha5 we know where it is deployed so you can call it whatever you want.

     

    You are not activating CDI in your deployment. You are adding two beans.xml, both in the wrong location.

     

     

    .addAsWebResource(new ByteArrayAsset("<beans/>".getBytes()), "beans.xml")
    .addAsManifestResource(new ByteArrayAsset("<beans/>".getBytes()), ArchivePaths.create("beans.xml"))
    

     

     

    It should be:

     

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

     

    EmptyAsset.INSTANCE is just a helper for a Empty File.

  • 6. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Aslak Knutsen Master

    No, sorry. you want back to Alpha4.

     

    Correct in Alpha4 should be addWebResource(...)

  • 7. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Jelle de Bruin Newbie

    Thanks again for the quick reply.

     

    It doesn't seem to matter. They are still NULL.

     

    My test class now looks like:

     

    @RunWith(Arquillian.class)

    @InitialPage("/index.xhtml")

    public class ArquillianTest {

     

        @Inject

        JSFServerSession server;

     

        @Inject

        JSFClientSession client;

     

        @Deployment

        public static WebArchive createDeployment() {

            System.setProperty("java.io.tmpdir", "c:\\data\\temp\\");

            System.setProperty("maven.repo.local", "c:\\repository");

     

            return ShrinkWrap.create(WebArchive.class, "test.war")

                    .setWebXML(new File("src/main/webapp/WEB-INF/web.xml"))

                    .addPackage(Package.getPackage("test.example"))

                    .addClasses(BackingBean.class)

                    .addResource(new File("src/main/webapp", "index.xhtml"))

                    .addWebResource(EmptyAsset.INSTANCE, "beans.xml")

                    .addManifestResource(new File("src/main/webapp/META-INF/context.xml"))

                    .addWebResource(new File("src/main/webapp/WEB-INF/faces-config.xml"), "faces-config.xml");

        }

     

        @Test

        public void testInitialPage() throws IOException {

            // Test navigation to initial viewID

            Assert.assertEquals("/index.xhtml", server.getCurrentViewID());

     

            // // Set the param and submit

            client.setValue("bic", "FRTGYU12345");

            client.click("submitButton");

     

            // Assert that the greeting component is in the component tree and

            // rendered

            UIComponent bicField = server.findComponent("bic");

            Assert.assertTrue(bicField.isRendered());

     

            // Test a managed bean using EL. We cheat and use the request object.

            Assert.assertEquals("FRTGYU12345", server.getManagedBeanValue("#{request.getParameter('bic')}"));

        }

     

    }

  • 8. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Aslak Knutsen Master

    What does the tomcat startup log say?

  • 10. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Jelle de Bruin Newbie

    I added the weld servlet to the deployment but it doesn't change anything.

     

    .addLibrary(MavenArtifactResolver.resolve("org.jboss.weld.servlet","weld-servlet","1.0.1-Final"))

     

    I'm getting the message that the BeanManager cannot be found but I added the context.xml and the resource refs to the web.xml:

     

    28-mrt-2011 16:50:15 org.jboss.arquillian.impl.XmlConfigurationBuilder loadArquillianConfiguration

    INFO: building configuration from XML file: arquillian.xml

    28-mrt-2011 16:50:15 org.apache.coyote.http11.Http11Protocol init

    INFO: Initializing Coyote HTTP/1.1 on http-127.0.0.1-8080

    28-mrt-2011 16:50:15 org.apache.catalina.startup.Embedded start

    INFO: Starting tomcat server

    28-mrt-2011 16:50:15 org.apache.catalina.core.StandardEngine start

    INFO: Starting Servlet Engine: Apache Tomcat/6.0.29

    28-mrt-2011 16:50:15 org.apache.catalina.connector.Connector initialize

    INFO: The connector has already been initialized

    28-mrt-2011 16:50:15 org.apache.coyote.http11.Http11Protocol start

    INFO: Starting Coyote HTTP/1.1 on http-127.0.0.1-8080

    28-mrt-2011 16:50:22 org.jboss.shrinkwrap.tomcat_6.api.ShrinkWrapStandardContext <init>

    INFO: Webapp archive location: c:\data\temp\export8535303944278814152test.war

    28-mrt-2011 16:50:22 org.apache.catalina.startup.ContextConfig defaultWebConfig

    INFO: No default web.xml

    28-mrt-2011 16:50:23 org.apache.catalina.core.StandardContext addApplicationListener

    INFO: The listener "com.sun.faces.config.ConfigureListener" is already configured for this context. The duplicate definition has been ignored.

    28-mrt-2011 16:50:23 org.jboss.weld.bootstrap.WeldBootstrap <clinit>

    INFO: WELD-000900 1.0.1 (Final)

    28-mrt-2011 16:50:23 org.jboss.weld.bootstrap.WeldBootstrap startContainer

    INFO: WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.

    28-mrt-2011 16:50:24 org.jboss.interceptor.model.InterceptionTypeRegistry <clinit>

    WARNING: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled

    28-mrt-2011 16:50:24 org.jboss.interceptor.model.InterceptionTypeRegistry <clinit>

    WARNING: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled

    28-mrt-2011 16:50:24 com.sun.faces.config.ConfigureListener contextInitialized

    INFO: Initializing Mojarra 2.1.0 (FCS 20110303) for context '/test'

    28-mrt-2011 16:50:26 org.jboss.arquillian.impl.XmlConfigurationBuilder loadArquillianConfiguration

    INFO: building configuration from XML file: arquillian.xml

    28-mrt-2011 16:50:26 org.jboss.arquillian.testenricher.cdi.CDIInjectionEnricher injectClass

    INFO: BeanManager cannot be located at java:comp/BeanManager. Either you are using an archive with no beans.xml, or the BeanManager has not been bound to that location in JNDI.

  • 11. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Jelle de Bruin Newbie

    Hi Aslak,

     

    I managed to get the BeanManager located. I changed the location of the context.xml and beans.xml as stated below.  

     

      @Deployment

        public static WebArchive createDeployment() {

            System.setProperty("java.io.tmpdir", "c:\\data\\temp\\");

            System.setProperty("maven.repo.local", "c:\\repository");

     

            return ShrinkWrap.create(WebArchive.class, "test.war")

                    .setWebXML(new File("src/main/webapp/WEB-INF/web.xml"))

                    .addPackage(Package.getPackage("test.example"))

                    .addClasses(BackingBean.class)

                    .addLibrary(MavenArtifactResolver.resolve("org.jboss.weld.servlet", "weld-servlet", "1.0.1-Final"))

                    .addResource(new File("src/main/webapp", "index.xhtml"))

                    .addResource(new File("src/main/webapp/META-INF/context.xml"), "META-INF/context.xml")

                    .addResource(new ByteArrayAsset(new byte[0]), "META-INF/beans.xml")

                    .addWebResource(new File("src/main/webapp/WEB-INF/faces-config.xml"), "faces-config.xml");

        }

     

    The problem I am now facing is that the BeanManager cannot lookup the JSFServerSession or JSFClientSession. I tried adding the jboss-jsfunit-core-2.0.0.Beta1 as a library to the deployment but it doesn't seem to change anything. Do you have any suggestions?

     

    Kind regards,

     

    Jelle

  • 12. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Aslak Knutsen Master

    Sorry, I didn't catch on. Tomcat 6 only supports Servlet 2.5, the JSFUnit Arq integration is based on Servlet 3.0 features, web-fragment.xml.

  • 13. Re: CDI problem using Arquillian with Tomcat 6.0 and JSFUnit
    Jelle de Bruin Newbie

    So what you are basically saying is that you cannot use Tomcat 6.0 with JSFunit and Arquillian.