Blogs

Blogs

Liferay Arquillian test cases using maven and spring

Liferay Arquillian test cases using maven and spring

Arquillian integrates seamlessly with familiar testing frameworks (e.g., JUnit, TestNG), allowing tests to be launched using existing IDE, Ant and Maven test plugins.

Arquillian can use three containers types 

1. Embedded: the test runner contains the container as a library

2. Managed: the test runner starts and stops the container as a separated process

3. Remote: the test runner relies on an operational container, already started.

There are a few steps required to setup and run Arquillian for plugin projects.

  • Setup Tomcat for Arquillian
  • Configure Project
  • Write Unit Test Case
  • Code coverage Report

Setup Tomcat for Arquillian

1. Adding Users to Tomcat

Arquillian will deploy the plugin inside the container which is how it has a runtime with which it can locate your dependencies from the test. 

Go to your $TOMCAT_HOME/conf directory and open the tomcat-users.xml file. 

If none exists, create it and add the following to the file  Save and close the file

   <?xml version="1.0"?>

            <tomcat-users>

        <role rolename="tomcat" />

        <role rolename="manager-gui" />

        <role rolename="manager-script" />

        <role rolename="manager-jmx" />

        <role rolename="manager-status" />

        <user password="tomcat" roles="tomcat, manager-gui,

  manager-script ,manager-jmx, manager-status"  

username="tomcat"/>

</tomcat-users>

2. Add Connection for Arquillian

Arquillian is going to use the manager application of Tomcat to deploy the plugin as part of the test execution, So modify environment properties to support it.

              Make sure that tomcat has manager folder in webapps directory.

  • Go to your $TOMCAT_HOME/bin directory and open the setenv.sh (if you are on MAC/Linux) or setenv.bat (if you are on Windows)

  • Add the following to the file.

JMX_OPTS="-Dcom.sun.management.jmxremote  -Dcom.sun.management.jmxremote.authenticate=false  -Dcom.sun.management.jmxremote.port=8099  -Dcom.sun.management.jmxremote.ssl=false"

       CATALINA_OPTS="${CATALINA_OPTS} ${JMX_OPTS}"

You can alter port to use a different value other than 8099 – but this is the "default" value.

Restart your TOMCAT server to make sure the changes are picked up.

 

Configure Project

 1. Open the pom.xml for your project and add below dependencies

<!-- Test Dependencies -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

</dependency>

<dependency>

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

<artifactId>arquillian-tomcat-remote-7</artifactId>

<version>1.0.0.CR7</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.jboss.shrinkwrap.resolver</groupId>

<artifactId>shrinkwrap-resolver-impl-maven</artifactId>

<scope>test</scope>

</dependency>

<dependency>

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

<artifactId>arquillian-junit-container</artifactId>

<scope>test</scope>

</dependency>

Note: arquillian-tomcat-remote-7 is for Container type Remote for Managed use arquillian-tomcat-managed-7 and for embedded use arquillian-tomcat-embedded-7 maven dependencies

2. Add the following dependency management node just above your listed dependencies

<dependencyManagement>

<dependencies>

<dependency>

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

<artifactId>arquillian-bom</artifactId>

<version>1.1.11.Final</version>

<scope>import</scope>

<type>pom</type>

</dependency>

</dependencies>

</dependencyManagement>

3 .Now add a folder called java under the /src/test folder.

4. Add a new folder resources and create a new file called arquillian.xml and place the following contents (adjusting values based on your settings).

  • Arquillian settings for Remote type container :

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

<arquillian xmlns="http://jboss.org/schema/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/deployments</property>

    </engine>

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

        <configuration>

            <property name="user">tomcat</property>

            <property name="pass">tomcat</property>

        </configuration>

    </container>    

</arquillian>

  • Arquillian settings for Managed type container 

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

        <configuration>

            <property name="user">tomcat</property>

            <property name="pass">tomcat</property>

            <property name="catalinaHome">Path of Tomcate folder</property>

            <property name="catalinaBase">Path of Tomcate folder</property>

        </configuration>

    </container>

 

Write Unit Test Case

Click on the /test/java folder and create new test classes  ArquillianDeployment.java and MyArquillianPortletTest.java

  • ArquillianDeployment.java is a generic class which is used to define a deployment type of test cases (ex : war or jar).

package com.arquillian.portlet.test;

import java.io.File;

import org.jboss.shrinkwrap.api.ShrinkWrap;

import org.jboss.shrinkwrap.api.spec.WebArchive;

import org.jboss.shrinkwrap.resolver.api.maven.Maven;

import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage;

public class ArquillianDeployment {

private static final String LOCAL_WEBAPP_DIR = "src/main/webapp/WEB-INF";

public static WebArchive createDeployment() {

PomEquippedResolveStage mavenResolver =  Maven.resolver().loadPomFromFile("pom.xml");

File[] libs = mavenResolver.importRuntimeAndTestDependencies().resolve().withTransitivity().asFile();

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

.addPackages(true,"com.arquillian.portlet.test","com.arquillian.controller")

.addAsWebInfResource(new File(LOCAL_WEBAPP_DIR, "portlet.xml"))

.addAsWebInfResource(new File(LOCAL_WEBAPP_DIR,    "liferay-plugin-package.properties"))

.addAsWebInfResource(new File(LOCAL_WEBAPP_DIR, "liferay-display.xml"))

.addAsWebInfResource(new File(LOCAL_WEBAPP_DIR, "liferay-portlet.xml"))

.addAsWebInfResource(new File(LOCAL_WEBAPP_DIR+"/spring-context/portlet",    "arquillian-portlet.xml"),"/spring-context/portlet/arquillian-portlet.xml")

.addAsWebInfResource(new File(LOCAL_WEBAPP_DIR+"/spring-context",  "portlet-application-context.xml"), 

"/spring-context/portlet-application-context.xml");

for (File file : libs) { war.addAsLibrary(file);}

return war;

}

}

  • MyArquillianPortletTest.java is Arquillian test class which used to create test methods

package com.arquillian.portlet.test;

import org.jboss.arquillian.container.test.api.Deployment;

import org.jboss.arquillian.junit.Arquillian;

import org.jboss.shrinkwrap.api.spec.WebArchive;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import com.liferay.portal.kernel.log.Log;

import com.liferay.portal.kernel.log.LogFactoryUtil;

import com.arquillian.controller.PortletViewController;

@RunWith(Arquillian.class) 

public class MyArquillianPortletTest {

private static Log LOGGER  = 

LogFactoryUtil.getLog(MyArquillianPortletTest.class);

private PortletViewController mvcPortlet;

private long userId=10101;

@Deployment(name="MyArquillian")

public static WebArchive init() {

return ArquillianDeployment.createDeployment();

}

@Before

public void setUp() throws Exception {

mvcPortlet = new PortletViewController();

}

  @Test

    public void testGetUserDetails(){

          mvcPortlet.getUserDetails(userId); 

     }

}

3. Save the file

4. Execute test cases by maven clean test.

 

Code Coverage Report using Jacoco

Jacoco is a free code coverage library for Java. it is very simple to add to all types of build including ANT and Maven.

  • Add Jacoco profile in pom.xml

<profiles>

<profile>

    <id>jacoco</id>

    <dependencies>

        <dependency>

            <groupId>org.jacoco</groupId>

            <artifactId>org.jacoco.core</artifactId>

            <version>0.7.4.201502262128</version>

            <scope>test</scope>

        </dependency>

        <dependency>

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

            <artifactId>arquillian-jacoco</artifactId>

            <version>1.0.0.Alpha8</version>

            <scope>test</scope>

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.jacoco</groupId>

                <artifactId>jacoco-maven-plugin</artifactId>

                <version>0.7.4.201502262128</version>

                <executions>

                    <execution>

                        <goals>

                            <goal>prepare-agent</goal>

                        </goals>

                   </execution>

                    <execution>

                        <id>report</id>

                        <phase>prepare-package</phase>

                        <goals>

                            <goal>report</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>

</profile>

  • Run unit test cases using maven clean test -Pjacoco jacoco:report

NOTE: 

  • Generated war of test classes will be store in \target\deployments folder of portlet
  • You can see report in \target\site\jacoco folder.

 

 

0