<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sonatype Blog &#187; junit</title>
	<atom:link href="http://www.sonatype.com/people/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sonatype.com/people</link>
	<description>Sonatype is transforming software development with tools, information and services that enable organizations to build better software, faster, using open-source components.</description>
	<lastBuildDate>Wed, 23 May 2012 14:24:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Integration Tests with Maven (Part 3): Case study &#8211; Flexmojos</title>
		<link>http://www.sonatype.com/people/2009/10/integration-tests-with-maven-part-3-case-study-flexmojos/</link>
		<comments>http://www.sonatype.com/people/2009/10/integration-tests-with-maven-part-3-case-study-flexmojos/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 15:40:35 +0000</pubDate>
		<dc:creator>Marvin Froeder</dc:creator>
				<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[failsafe]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[surefire]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=2636</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/10/integration-tests-with-maven-part-3-case-study-flexmojos/' addthis:title='Integration Tests with Maven (Part 3): Case study &#8211; Flexmojos '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>In the third part of this series I will talk about how integration tests (and unit tests) are done on Flexmojos. If you haven&#8217;t been following along, you should read the first and second posts of this series. Let&#8217;s start from the beginning, what is Flexmojos? Flexmojos is a maven plugin for Adobe Flex development. [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/10/integration-tests-with-maven-part-3-case-study-flexmojos/' addthis:title='Integration Tests with Maven (Part 3): Case study &#8211; Flexmojos '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>In the third part of this series I will talk about how integration tests (and unit tests) are done on Flexmojos. If you haven&#8217;t been following along, you should read the <a href="http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/">first</a> and <a href="http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/">second</a> posts of this series.</p>

<p><span id="more-2636"></span></p>

<p>Let&#8217;s start from the beginning, what is Flexmojos?
Flexmojos is a maven plugin for Adobe Flex development.  You can get acquainted with it <a href="http://flexmojos.sonatype.org/">here</a>.</p>

<h3>Some Background</h3>

<p>When I started Flexmojos I just wanted a decent maven plugin to build my project.  I never considered making it public or turning it into a open source project.  At the time, I just wanted something for me and for me alone.   As I continued to work on my own solution, I realized more people had problems building Flex projects using Maven.  So I decided to share my project.  This is how the Flexmojos project began, and once the project had some momentum we stared running into challenges that required rigorous integration testing.</p>

<p>After a few releases, Flexmojos started to present backward compatibility issues.  Something that worked on 1.0-alpha-2 suddenly stopped working on 1.0-alpha-3. And this started to happen more often during new features inclusion.  The obvious fix for that was to create a bunch of unit tests.  But <a href="http://maven.apache.org/guides/introduction/introduction-to-plugins.html">mojos</a> aren&#8217;t exactly the easiest things to unit test.  So I had to keep testing manually, at that point Maven and integration tests had too much complexity for my limited Maven API knowledge.  So I stopped including new features and concentrated on getting Flexmojos stable.  Eventually I managed to get 1.0 release out.</p>

<p>I saw how much time, releases and code I needed to get 1.0 stable and I wanted to get 2.0 stable w/o that much work.  But how?  Creating a good set of automated tests. But again, how to test mojos?  At this point, there wasn&#8217;t much information about testing Maven plugins.  Googling here and poking around there I found <a href="http://maven.apache.org/plugin-developers/plugin-testing.html">maven-verifier</a>.</p>

<blockquote>maven-verifier tests are run using JUnit or TestNG, and provide a simple class allowing you to launch Maven and assert on its log file and built artifacts. It also provides a ResourceExtractor, which extracts a Maven project from your src/test/resources directory into a temporary working directory where you can do tricky stuff with it.
</blockquote>

<p>That was perfect for my needs.  Using maven-verifier I found an easy way to test Flexmojos. In fact, I found a very good way to test Flexmojos, however when I started to write more complex tests it proved to be not as easy as I expected. We&#8217;ll talk more about that later. Using maven verifier is very cool too because it allowed me to write integration tests using TestNG, so I could use asserts, inherit, annotations and all the stuff available for unit testing that I was used to using.</p>

<h3> How are Flexmojos test organized </h3>

<p>Flexmojos only has integration tests.  Unit testing maven plugins is not easy and Flexmojos wasn&#8217;t designed in a particularly modular way.  If it were, it would have more unit tests.</p>

<p>Since Flexmojos is a Maven plugin I needed it installed into a Maven repository to run the integration tests, for that reason Flexmojos has all integration tests grouped into a single project, called <a href="http://svn.sonatype.org/flexmojos/trunk/flexmojos-testing/flexmojos-test-harness/">flexmojos-test-harness</a>.</p>

<p>The flexmojos-test-harness creates a complete environment to run the integration tests:
 &#8211; Download and unpack Maven 2.0.9 from central;
 &#8211; Overwrite Maven settings.xml with one that contains extra repositories ( <a href="https://svn.sonatype.org/flexmojos/trunk/flexmojos-testing/flexmojos-test-harness/src/test/resources/settings.xml">check it here</a> ), with all required artifacts;
 &#8211; Install Flexmojos artifacts into a fake local repository; this allow us to
 &#8211; Instrument Flexmojos artifacts using Emma;
 &#8211; Add emma.jar to Maven libs directory (required to enable test coverage).</p>

<p>This project sets up everything necessary to run Flexmojos (install maven), it sets up Emma (instrumenting and add emma.jar) and makes sure the right version of the artifacts are used during tests (filling maven repo).</p>

<h2>Motivation for Flexmojos Integration Testing</h2>

<p><strong>Q.</strong> <em>Why download and unpack maven from central, instead of using the maven already present on machine?</em>
<strong>A.</strong> First and most important reason: Instrumented classes need emma.jar on classpath.  If not present, it will throw ClassDefNotFoundExceptions.  So I can tamper with this Maven instance created by Flexmojos adding emma.jar.  And that is a good reason to ensure that Flexmojos can manage a separate Maven installation that is isolated from all of the variation of developers who might be testing Flexmojos.  At any moment I can set another version to be sure that Flexmojos is still working.</p>

<p><strong>Q.</strong> <em>Why overwrite <a href="https://svn.sonatype.org/flexmojos/trunk/flexmojos-testing/flexmojos-test-harness/src/test/resources/settings.xml">settings.xml</a>?</em>
<strong>A.</strong> First of all, it was was necessary to add the Flexmojos repository, so that maven could download the flex compiler from the server.  This modified settings.xml does include a reference to an isolated, local Maven repository, so that anyone running the integration tests will retrieve artifacts already available on current machine, this dramatically reduces the time required to run the integration tests.  Also notice that this settings.xml blocks all snapshots from being downloaded, that is nice because it prevents instrumented Flexmojos being overwritten with non-instrumented versions from maven central.</p>

<p><strong>Q.</strong> <em>Why put the artifacts on a fake local repository?</em>
<strong>A.</strong> That is about isolation&#8230;  if I used the machine&#8217;s local repository, Flexmojos artifacts would get instrumented by Emma.  But that would break Flexmojos.  It&#8217;s required to add emma.jar on Maven classpath just to run instrumented Flexmojos.  But shipping instrumented Flexmojos gives users no advantage, quite the opposite, it would generate coverage reports every time, wasting CPU, disk and memory usage.  Another reason is that would break the GPG signature for Flexmojos artifacts.  So, creating a isolated local repository allowed me to run instrumented tests without messing with built artifacts.</p>

<h2>How to Configure Integration Tests</h2>

<p><strong>Q.</strong> <em>How do you download and unpack maven from central?</em>
<strong>A.</strong> To copy and unpack artifacts is used the <a href="http://maven.apache.org/plugins/maven-dependency-plugin/">maven-dependency-plugin</a>.  This plugin &#8220;provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location.&#8221;</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>extract-maven<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>generate-test-resources<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>unpack<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactItems<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>apache-maven<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${maven.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tar.gz<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;classifier<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>bin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/classifier<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactItems<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${project.build.directory}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<p>Read more about unpack at plugin site.  <a href="http://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html">Unpack goal documentation</a> and <a href="http://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html">more examples</a>.</p>

<p><strong>Q.</strong> <em>How does settings.xml know where to find the local Maven repository?</em>
<strong>A.</strong> The settings.xml used on tests has some variables, namely <em>${localRepository.basedir}</em>, that links to default repository location, and <em>${maven.repo.local}</em>, that has the location when default-value is overwritten.  This variables are interpolated using <a href="http://maven.apache.org/plugins/maven-resources-plugin/">maven-resource-plugin</a>.
To enabled the variables interpolation is only necessary to declare the resources with the filtering option set to true, like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testResources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testResource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>src/test/resources<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filtering<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filtering<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>settings.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;targetPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${fake-maven}/conf<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/targetPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testResource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testResources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<p>That was not declared on maven-resource-plugin configuration, but on build section, under resources or testResources (Flexmojos ITs case).</p>

<p><strong>Q.</strong> <em>How does the resources directory get interpolated? And why isn&#8217;t maven-resource-plugin used?</em>
<strong>A.</strong> That is a performance question&#8230;. maven-resource-plugin would always interpolate all files, and there are several files under resources, that alone was taking 30 seconds to a minute.  But when running individual tests, not all files are required, so Flexmojos implemented its own interpolation, which runs before the test. Take a look at <a href="http://svn.sonatype.org/flexmojos/trunk/flexmojos-testing/flexmojos-test-harness/src/test/java/org/sonatype/flexmojos/tests/AbstractFlexMojosTests.java">AbstractFlexmojosTests.getProject()</a></p>

<h2>Code coverage</h2>

<p>For the last part of this blog, I show how Flexmojos generates a test coverage report.  Flexmojos uses emma4it-maven-plugin (introduced on <a href="http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/">part 2</a> of this blog series).</p>

<p>Emma4it has a goal to merge all available Emma binaries into a single one.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.sonatype.maven.plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>emma4it-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>merge<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>verify<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>merge<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;searchPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${basedir}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/searchPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<p>This way, it is possible to combine coverage results of unit and integration tests, it becomes more relevant on Flexmojos because we have being improving UT tests coverage, especially in some new components that are very easy to unit-test (like the new generation and testing API).</p>

<p>In part 4 we will talk a bit about ITs on <a href="http://nexus.sonatype.org/">Nexus</a>.  Nexus does have a very particular setup where the approach used for Flexmojos proved to be inefficient.</p>

<p>See you in the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sonatype.com/people/2009/10/integration-tests-with-maven-part-3-case-study-flexmojos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integration tests with Maven (Part 1): Failsafe Plugin</title>
		<link>http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/</link>
		<comments>http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 15:40:08 +0000</pubDate>
		<dc:creator>Marvin Froeder</dc:creator>
				<category><![CDATA[Maven]]></category>
		<category><![CDATA[failsafe]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[surefire]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=2408</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/' addthis:title='Integration tests with Maven (Part 1): Failsafe Plugin '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>Everyone knows Maven is great for running unit tests, and it is usually one of the first things that people learn when they are adopting Maven as a technology. Integration tests are another matter, and require a more detailed introduction. In this series of articles, I will explain how to set up integration tests in [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/' addthis:title='Integration tests with Maven (Part 1): Failsafe Plugin '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>Everyone knows Maven is great for running unit tests, and it is usually one of the first things that people learn when they are adopting Maven as a technology.   Integration tests are another matter, and require a more detailed introduction.  In this series of articles, I will explain how to set up integration tests in Maven starting the series by testing a simple jar and then advancing into more complex scenarios.</p>

<p><span id="more-2408"></span></p>

<h3>The application</h3>

<p>Let&#8217;s define a very simple application to be tested.  The application will return an exit code equal to the number of command-line parameters.  Running the program with no parameters will produce an exit code equal to zero, two parameters exit code two, and so on.</p>

<p>The application:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.sonatype.simpleclientapp</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span> execute<span style="color: #009900;">&#40;</span> args <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> execute<span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> args <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> args.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>This class is very simple to test, in the next section you will see how you can write a simple integration test using JUnit.</p>

<h3>Creating and running integration tests</h3>

<p>Let&#8217;s create a simple integration test using JUnit:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.sonatype.simpleclientapp</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">junit.framework.TestCase</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainIT
    <span style="color: #000000; font-weight: bold;">extends</span> TestCase
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testExecute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        assertEquals<span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">0</span>, execute<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertEquals<span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">1</span>, execute<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;one&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertEquals<span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">6</span>, execute<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #0000ff;">&quot;two&quot;</span>, <span style="color: #0000ff;">&quot;three&quot;</span>, <span style="color: #0000ff;">&quot;four&quot;</span>, <span style="color: #0000ff;">&quot;five&quot;</span>, <span style="color: #0000ff;">&quot;six&quot;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> execute<span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args <span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">File</span> jar <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;target/simple-client-app-1.0-SNAPSHOT.jar&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> execArgs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">arraycopy</span><span style="color: #009900;">&#40;</span> args, <span style="color: #cc66cc;">0</span>, execArgs, <span style="color: #cc66cc;">3</span>, args.<span style="color: #006633;">length</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        execArgs<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;java&quot;</span><span style="color: #339933;">;</span>
        execArgs<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;-jar&quot;</span><span style="color: #339933;">;</span>
        execArgs<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> jar.<span style="color: #006633;">getCanonicalPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Process</span> p <span style="color: #339933;">=</span> <span style="color: #003399;">Runtime</span>.<span style="color: #006633;">getRuntime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">exec</span><span style="color: #009900;">&#40;</span> execArgs <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        p.<span style="color: #006633;">waitFor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> p.<span style="color: #006633;">exitValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Note that this test needs the packaged jar (simple-client-app-1.0-SNAPSHOT.jar), which isn&#8217;t created when surefire runs the unit tests.</p>

<p>To run this we need a different approach. By different approach I mean a different plugin: the <a href="http://mojo.codehaus.org/failsafe-maven-plugin/">Failsafe Maven Plugin</a>.  The Failsafe Plugin is a fork of the Surefire plugin designed to run integration tests.</p>

<p>The Failsafe plugin goals are designed to run after the package phase, on the integration-test phase.</p>

<p>The Maven lifecycle has four phases for running integration tests:</p>

<ul>
<li>pre-integration-test: on this phase we can start any required service or do any action, like starting a database, or starting a webserver, anything&#8230; you can think on this as Junit.setUp()</li>
<li>integration-test: failsafe will run the test on this phase, so after all required services are started.</li>
<li>post-integration-test: time to shutdown all services&#8230; you can think of this as Junit.tearDown()</li>
<li>verify: failsafe runs another goal that interprets the results of tests here, if any tests didn&#8217;t pass failsafe will display the results and exit the build.</li>
</ul>

<p>Configuring Failsafe in the POM:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.codehaus.mojo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>failsafe-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.4.3-alpha-1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>integration-test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>verify<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<p>By default, the Surefire plugin executes **/Test&#42;.java, **/&#42;Test.java, and **/&#42;TestCase.java test classes.    The Failsafe plugin will look for **/IT&#42;.java, **/&#42;IT.java, and **/&#42;ITCase.java.  If you are using both the Surefire and Failsafe plugins, make sure that you use this naming convention to make it easier to identify which tests are being executed by which plugin.</p>

<p>In the next part of this series, I will talk about test coverage and demonstrate a method of including coverage of both Unit Tests and Integration Tests in a report that can be generated for a Maven project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

