3 Replies Latest reply on Jul 26, 2011 5:59 AM by Aslak Knutsen

    Arquillian + FacesContext

    bartoszl Newbie

      Hello everyone,

       

      Please help me,

      I would like to test the following class/method:

       

      @SessionScoped

      @ManagedBean

      public class UserManagedBean {

       

          private FacesContext facesContext;

          private String username;

          private String password;

       

           .....

       

            //getters and setters

       

           ....

       

           /**

             * @return

           */

          public String login() {

              facesContext = FacesContext.getCurrentInstance();

              Locale locale = facesContext.getViewRoot().getLocale();

              ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);

              if ("admin".equalsIgnoreCase(getUsername()) && "admin".equals(getPassword())) {

                  return "home";

              } else {

                  facesContext.addMessage("username", new FacesMessage(bundle.getString("invalid.user")));

                  return "login";

              }

          }

       

         .....

       

      }

       

       

      I have tested the class as follows:

       

      @RunWith(Arquillian.class)

      public class UserManagedBeanTest {

          @Inject

          private UserManagedBean userManagedBean;

          @Deployment

          public static WebArchive createTestArchive() {

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

                      addClasses(UserManagedBean.class, UserService.class, User.class).

                      addClasses(FacesServlet.class, FacesException.class, FacesMessage.class).

                      addAsResource(new File("src/main/resources/messages_pl.properties")).

                      addAsResource(new File("src/main/resources/messages_en.properties")).

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

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

                      addAsManifestResource(new ByteArrayAsset(new byte[0]), ArchivePaths.create("beans.xml"));

              System.out.println(war.toString(true)); // for debugging

              return war;

          }

        

       

          @Test

          public void testLogin() {

              userManagedBean = new UserManagedBean();

              userManagedBean.setUsername("admin");

              userManagedBean.setPassword("admin");

              String expResult = "home";

              String result = userManagedBean.login();

              assertEquals(expResult, result);

          }


      }

       

      Arquillian.xml this file as follows:

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

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

           <engine>

              <property name="deploymentExportPath">target/</property>

          </engine>

      </arquillian>

       

      The pom.xml file is as follows:

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

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

          <groupId>test</groupId>

          <artifactId>test-web</artifactId>

          <version>1.0-SNAPSHOT</version>

          <packaging>war</packaging>

          <name>test-web Web App</name>

       

          <properties>

              <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>

              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

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

              <jboss.version>6.0.0-Final</jboss.version>

          </properties>

       

          <dependencies>

              <dependency>

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

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

                  <version>1.0.0.Final</version>

                  <type>pom</type>

                  <scope>provided</scope>

              </dependency>

              <!-- PrimeFaces  -->

              <dependency>

                      <groupId>org.primefaces</groupId>

                      <artifactId>primefaces</artifactId>

                      <version>2.2</version>

              </dependency>

               <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.slf4j</groupId>

                  <artifactId>slf4j-api</artifactId>

                  <version>1.6.1</version>

                  <scope>test</scope>

              </dependency>

          </dependencies>

          <build>

              <outputDirectory>target/classes</outputDirectory>

              <plugins>

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-compiler-plugin</artifactId>

                      <version>2.3.1</version>

                      <configuration>

                          <source>1.6</source>

                          <target>1.6</target>

                          <compilerArguments>

                              <endorseddirs>${endorsed.dir}</endorseddirs>

                          </compilerArguments>

                      </configuration>

                  </plugin>

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-war-plugin</artifactId>

                      <version>2.1.1</version>

                      <configuration>

                          <failOnMissingWebXml>false</failOnMissingWebXml>

                          <webResources>

                              <resource>

                                  <directory>${basedir}/src/main/java</directory>

                                  <targetPath>/WEB-INF/src</targetPath>

                              </resource>

                          </webResources>

                      </configuration>

                  </plugin>

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-dependency-plugin</artifactId>

                      <version>2.1</version>

                      <executions>

                          <execution>

                              <phase>validate</phase>

                              <goals>

                                  <goal>copy</goal>

                              </goals>

                              <configuration>

                                  <outputDirectory>${endorsed.dir}</outputDirectory>

                                  <silent>true</silent>

                                  <artifactItems>

                                      <artifactItem>

                                          <groupId>javax</groupId>

                                          <artifactId>javaee-endorsed-api</artifactId>

                                          <version>6.0</version>

                                          <type>jar</type>

                                      </artifactItem>

                                  </artifactItems>

                              </configuration>

                          </execution>

                      </executions>

                  </plugin>

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-resources-plugin</artifactId>

                      <version>2.3</version>

                  </plugin>

              </plugins>

              <resources>

                  <resource>

                      <directory>src/main/resources</directory>

                      <includes>

                          <include> **/*.properties</include>

                      </includes>

                  </resource>

              </resources>

          </build>

           <profiles>

              <profile>

                  <id>jbossas-remote-6</id>

                  <dependencies>

                      <dependency>

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

                          <artifactId>arquillian-jbossas-remote-6</artifactId>

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

                          <scope>test</scope>

                      </dependency>

                      <dependency>

                          <groupId>org.jboss.jbossas</groupId>

                          <artifactId>jboss-as-profileservice-client</artifactId>

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

                          <type>pom</type>

                      </dependency>

                  </dependencies>

              </profile>

          </profiles>

       

      </project>

       

       

      When I run the JUnit test in Netbeans7 I get the following message:

       

      org.jboss.arquillian.impl.client.container.ContainerRegistryCreator getActivatedConfiguration

      INFO: Could not read active container configuration: null

       

      Tests in error:

        testLogin(pl.bsb.sample.UserManagedBeanTest)

       

      and

       

      testLogin(UserManagedBeanTest)  Time elapsed: 0.197 sec  <<< ERROR!

      java.lang.NullPointerException

      [ on the line " Locale locale = facesContext.getViewRoot().getLocale();" - because facesContext not been initialized ]

       

       

      What is wrong?