Liferay 6.2 Integration Tests with Arquillian and maven

Blog posts

Liferay 6.2 Integration Tests with Arquillian and maven

Posted on 06/06/2016 by Charalampos Chrysikopoulos

At last, after almost two years, I managed finally to make the integration tests work. And it is not so complicated as it seems to be. So straight to the point.

I needed to enabled integration tests in Liferay 6.2EE SP14, but, I think, the steps are the same for all the 6.2 versions (also the CE).

Arquillian is a framework that help us to run our test in the liferay portal, so we don't need to mock it.

So, we need to configure our installation to work with arguillian. I suppose we have a tomcat installed on our system. We need to make some configurations to that tomcat, so arquillian can communicate with it and deploy its stuff.

Configure Tomcat

Installing Manager
If you look at the webapps directory of your tomcat, per default, the manager web application is missing. You can add it, simple, by downloading the same tomcat version and from its webapps directory you can copy the manager folder back to you tomcats webapps directory.

Setting users
Having the manager web application installed, we need to define a user, that arquillian will be use, to deploy its plugins, by using the manager. To achieve this, we need to define a user with the appropriate roles. So, we need to edit the file tomcat-users.xml in TOMCAT_DIR/conf directory.

In there we need to insert the following xml:

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

Enabling JMX
Now, we need to enable JMX in tomcat. We edit the setenv.sh (or setenv.bat for windows) file in TOMCAT_DIR/bin directory. I replaced the content with the following:

CATALINA_OPTS="$CATALINA_OPTS -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true  -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false -Duser.timezone=GMT -Xmx1024m -XX:MaxPermSize=256m -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=8099 -Dcom.sun.management.jmxremote.ssl=false"

Now, the tomcat configuration has been set.

Configure Maven

In our plugin we need to add some dependencies.

We will use the arquillian-liferay-maven-extension.

In the plugins pom.xml file I added the following:

        ...
        </build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.5.Final</version>
<scope>test</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.arquillian.liferay.maven</groupId>
<artifactId>arquillian-liferay-maven-extension</artifactId>
<version>1.0.0.Alpha2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-tomcat-remote-7</artifactId>
<version>1.0.0.CR7</version>
<scope>test</scope>
</dependency>
                ....
       <dependencies>
       ...

Configure Arquillian

We need to give arquillian the information, where it can run the tests. So, we add the file arquillian.xml in the folder src/test/java/integration. In the file we add following XML:

<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
        <container qualifier="tomcat" default="true">
                <configuration>
                        <property name="jmxPort">8099</property>
                        <property name="host">localhost</property>
                        <property name="httpPort">8080</property>
                        <property name="user">tomcat</property>
                        <property name="pass">tomcat</property>
                </configuration>
        </container>
</arquillian>

Here we are using all the previous things we configured.

The integration test

Now, we are ready to write our integration test. Lets assume, we have a custom Entity with the name SomeEntity. Lets try the following in our plugin project. We create a new class SomeEntityLocalServiceUtilTest in src/test/java/somewhere/service/impl.

import somewhere.service.model.SomeEntity;
import somewhere.service.service.SomeEntityLocalServiceUtil;
 
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
 
@RunWith(Arquillian.class)
public class SomeEntityLocalServiceUtilTest {
 
@Test
public void test() {
 
int count = SomeEntityLocalServiceUtil.getSomeEntitiesCount();
 
Assert.assertTrue(count == 0);
}
}

Run the test

At last, we can now see, if our test works. Don't forget, the tomcat should be running. Let's run the test:

mvn test

We should see how arquillian builds the plugin, deploys it on the portal, run the test and undeploys it again.

Notes

  • It seems to be correct to have a test server to run your tests. Not only for the db data, but arquillian deploys and then undeploys the plugins you want to test. So, while developing, perhaps you make also your own deployments in the portal, and this could create unwanted conflicts.
  • Sometimes I get java.lang.OutOfMemoryError: PermGen space. I didn't solve it yet. It seems, that arquillian starts a jvm (with surefire), and I couldn't find a way to define memory parameters there.
  • Running the tests from jenkins, there was a problem with a library, so I needed to add the following dependency in the pom.xml file:
    <dependency>
    <groupId>biz.aQute.bnd</groupId>
    <artifactId>bnd</artifactId>
    <version>2.4.0</version>
    </dependency>
This entry was posted in Liferay and tagged arguillian, integration-test, liferay 6.2, test by Charalampos Chrysikopoulos

Comments

Add new comment

You must have Javascript enabled to use this form.