Wednesday, April 9, 2014

Automated Arquillian Test with Managed Tomcat

I recent faces this issue. In my test code, I use JMockit for Mock and Arquillian to drive the server. In the beginning I used embedded tomcat for test. But there is one problem, Arquillian runs both Server(embedded Tomcat) and client code in the same JVM. If one class is mocked, it is mocked in both client and server. But I only want to mock at client side. I need to use Managed Tomcat for test. But  I do not want the hassle to manage the server. Moreover,  when code is downloaded by others, or tested in CI server, I want to the test to be run in an automatic fashion. To work towards this end, I automate the Tomcat download and configuration process.

First, use the maven-cargo plugin to download Tomcat automatically
    <!-- launch tomcat, deploy application before integration test -->
                    <!-- install tomcat
                    So it could be used by arquillion managed

  <!-- Container configuration -->

Then configure the download tomcat so that it can be managed remotely by Arquillian
<!--  copy customized tomcat configuration -->
                    <!-- here the phase you need -->

Here you prepare the configuration under /src/test/resources/tomcat7x/conf. They will be copied to installed Tomcat by this plugin.

Prepare the arquillian configuration so it knows how to communicate with managed tomcat: src/test/resources/arquillian.xml
<arquillian xmlns=""
    <container qualifier="tomcat-managed" default="true">
            <property name="startupTimeoutInSeconds">20</property>
            <!-- The port to connect -->
            <property name="bindHttpPort">8888</property>
            <!--  the port to connect -->
<!--             <property name="jmxPort">8089</property> -->
            <property name="catalinaHome">target/apache-tomcat-7.0.52/apache-tomcat-7.0.52</property>
            <property name="outputToConsole">true</property>
             <property name="user">tomcat</property>
            <property name="pass">manager</property>
             <property name="host">localhost</property>

The name of this configuration is tomcat-managed here.
Finally Let maven-surefire to use this arquillian configuration
    <!-- Fire unit test -->
                <configuration>                        <arquillian.launch>tomcat-managed</arquillian.launch>

Do not forget let arqillian to launch your managed tomcat for you.

1 comment:

  1. My Rank, the only portal which offers jee mains online practice test with unlimited chapter wise practice test along with ask your query and live chat options. The portal is also loaded with WhiteBoard (live online classes by IITian faculty)