<?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; testing</title>
	<atom:link href="http://blog.sonatype.com/people/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.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>Thu, 16 May 2013 18:53:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Integration Tests with Maven (Part 3): Case study &#8211; Flexmojos</title>
		<link>http://blog.sonatype.com/people/2009/10/integration-tests-with-maven-part-3-case-study-flexmojos/</link>
		<comments>http://blog.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[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[<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://blog.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>Profiling Maven Tests</title>
		<link>http://blog.sonatype.com/people/2009/09/profiling-maven-tests/</link>
		<comments>http://blog.sonatype.com/people/2009/09/profiling-maven-tests/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 12:02:30 +0000</pubDate>
		<dc:creator>Marvin Froeder</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=2763</guid>
		<description><![CDATA[A few days ago we changed nexus integration tests to use an embedded version of nexus instead of using a forked VM. Before every test Nexus is started and then stopped again after the test completes. This is done once for each test class (Junit: @BeforeClass=start nexus, @AfterClass=stop nexus). During the first run we noticed [...]]]></description>
				<content:encoded><![CDATA[<p>A few days ago we changed nexus integration tests to use an embedded version of nexus instead of using a forked VM.  Before every test Nexus is started and then stopped again after the test completes.  This is done once for each test class (Junit: @BeforeClass=start nexus, @AfterClass=stop nexus).</p>

<p>During the first run we noticed some problems.Each time Nexus started, VM would use an additional 45Mb of memory.  Soon we realized that our embedder was not working right.  Stopping Nexus was not removing it from memory.</p>

<p>Setting all fields to null after stopping helped somewhat.  But now instead of 45Mb of leak we were getting 40Mb, which was still too much when we had more then 200 tests to run. It was time to profile our tests.</p>

<p>At Sonatype we use <a href="http://www.yourkit.com/">yourkit</a> for profiling, so I will demonstrate how to use yourkit to profile tests executed by maven. You should be able to use the same principle with any other profile tool.
<span id="more-2763"></span>
1 &#8211; First, <a href="http://www.yourkit.com/download/index.jsp">download, get a license</a> and install yourkit.
2 &#8211; Create a new profile to hold yourkit-related configuration:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profiles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #808080; font-style: italic;">&lt;!-- http://www.yourkit.com/docs/80/help/agent.jsp --&gt;</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>yourkit-profile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;yourkit.home<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>C:\Arquivos de programas\YourKit Java Profiler 8.0.13<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/yourkit.home<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profiles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<p>I put the yourkit.home in pom.xml, but you can have it in settings.xml as well.</p>

<p>3 &#8211; Add yourkit agent to surefire inside the profile:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profile<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>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<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>
            <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.plugins<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>maven-surefire-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;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argLine<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-agentpath:&quot;${yourkit.home}\bin\win32\yjpagent.dll&quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argLine<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;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<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>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<p>There are other ways to do this you can see <a href="http://www.yourkit.com/docs/80/help/agent.jsp">here</a>.</p>

<p>4 &#8211; Launch yourkit UI.</p>

<div id="attachment_2765" class="wp-caption alignnone" style="width: 160px"><img class="size-thumbnail wp-image-2765" title="ui" src="http://www.sonatype.com/people/wp-content/uploads/2009/08/ui-150x150.png" alt="Yourkit UI" width="150" height="150" /><p class="wp-caption-text">Yourkit UI</p></div>

<p>5 &#8211; Launch maven using yourkit profile:
$ mvn clean test -P yourkit-profile -Dyourkit.home={your yourkit installation home}</p>

<p>As soon as yourkit UI detects your application, it will begin collecting profiling data.</p>

<div id="attachment_2766" class="wp-caption alignnone" style="width: 160px"><img class="size-thumbnail wp-image-2766" title="loaded-ui" src="http://www.sonatype.com/people/wp-content/uploads/2009/08/loaded-ui-150x150.png" alt="Test running" width="150" height="150" /><p class="wp-caption-text">Test running</p></div>

<h5>Optional, using the API</h5>

<p>Regular profiling is not very useful to find memory leaks on unit tests.  A much more efficient approach is to take memory snapshots programmatically.  To do so, we need <a href="http://www.yourkit.com/docs/80/help/api.jsp">yourkit API</a>.</p>

<p>On the same profile, add this dependency:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<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>com.yourkit<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>yjp<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>8.0.13<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;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>system<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;systemPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${yourkit.home}\lib\yjp.jar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/systemPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<p>Note that you should not move yjp.jar to your local maven repository.</p>

<p>Now we can create a <em>com.yourkit.api.Controller</em> instance and take memory snapshos when we need to.</p>

<p>To avoid compilation problems during regular builds (when you are not profiling) I suggest doing that using reflection, like this:</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// profiling with yourkit, activate using -P youtkit-profile</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Object</span> profiler<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> startProfiler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">Class</span> controllerClazz<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            controllerClazz <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;com.yourkit.api.Controller&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Exception</span> e <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Profiler not present&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            profiler <span style="color: #339933;">=</span> controllerClazz.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            controllerClazz.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;captureMemorySnapshot&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span> profiler <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Exception</span> e <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            fail<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Profiler was active, but failed due: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> takeSnapshot<span style="color: #009900;">&#40;</span><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> profiler <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;">try</span>
            <span style="color: #009900;">&#123;</span>
                profiler.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;forceGC&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span> profiler <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                profiler.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;captureMemorySnapshot&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span> profiler <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Exception</span> e <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                fail<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Profiler was active, but failed due: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>


<p>That way you won&#8217;t need to change the code to run in non-profiling mode.</p>

<p>You can see the full example in place at <a href="https://svn.sonatype.org/nexus/trunk/nexus/nexus-test-harness/nexus-test-harness-launcher">nexus-test-harness</a>.</p>

<p>If you are still reading and are curious what was causing the memory leak on Nexus tests, we discovered that jetty creates Shutdown Hook that can hold a reference to all servers (depends on how you configure it).  That shutdown hook stops jetty instances when the VM is going down.  So it was just a matter of removing my jetty instances from it when I manually stopped Nexus.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2009/09/profiling-maven-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integration Tests with Maven (Part 2): Test Coverage Reports</title>
		<link>http://blog.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/</link>
		<comments>http://blog.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 16:05:17 +0000</pubDate>
		<dc:creator>Marvin Froeder</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[emma]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=2475</guid>
		<description><![CDATA[In the previous article, we demonstrated one way of running integration tests with Maven. In this article, we will demonstrate how to measure the test coverage of both integration and unit tests. We will be using Emma to measure and report the coverage. EMMA is an open-source toolkit for measuring and reporting Java code coverage. [...]]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/">previous article</a>, we demonstrated one way of running integration tests with Maven.  In this article, we will demonstrate how to measure the test coverage of both integration and unit tests.</p>

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

<p>We will be using Emma to measure and report the coverage.    EMMA is an open-source toolkit for measuring and reporting Java code coverage.</p>

<h3>Unit test coverage</h3>

<p>Let&#8217;s start by generating a coverage report for unit tests only. This will require 2 new plugins to be added to our pom.  The first one:</p>

<pre style="padding-left: 30px;">&lt;plugin&gt;
  &lt;groupId&gt;org.sonatype.maven.plugin&lt;/groupId&gt;
  &lt;artifactId&gt;emma-maven-plugin&lt;/artifactId&gt;
  &lt;version&gt;1.1&lt;/version&gt;
  &lt;executions&gt;
    &lt;execution&gt;
      &lt;phase&gt;process-classes&lt;/phase&gt;
      &lt;goals&gt;
        &lt;goal&gt;instrument&lt;/goal&gt;
      &lt;/goals&gt;
    &lt;/execution&gt;
  &lt;/executions&gt;
&lt;/plugin&gt;</pre>

<p><span>This plugin will instrument classes at target/generated-class/emma/classes, this won&#8217;t change the original classes at target/classes.   Then, we must tell surefire plugin to look for classes at new location:</span></p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  org.apache.maven.plugins
  maven-surefire-plugin
  2.4.3
&nbsp;
    ${project.build.directory}/generated-classes/emma/classes</pre></div></div>


<p>At this point, we do 50% of code coverage. Mainly because the psvm ( public static void main ) is never executed.</p>

<p>Here is the EMMA text report.  Emma does produce xml and html reports in addition to this simple text report:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">[EMMA v2.0.5312 report, generated Fri Jun 12 15:43:28 BRT 2009]
-------------------------------------------------------------------------------
OVERALL COVERAGE SUMMARY:
&nbsp;
[class, %]	[method, %]	[block, %]	[line, %]	[name]
100% (1/1)	67%  (2/3)!	56%  (10/18)!	50%  (4/8)!	all classes</pre></div></div>


<p><strong>Note:</strong> at this point no report is generated, we will configure report generation after integration tests coverage reports are in place.   Once you run Surefire, you may notice the <em>coverage.ec</em> in the project root directory.  This file contains the coverage result in a binary format used by Emma.</p>

<h3>Integration test coverage</h3>

<p>So far, we have the unit tests results.  As I demonstrated in the previous article in this series,  the integration tests need the project artifact to run, so, we need to instrument this jar in order to get coverage result on ITs.  Let&#8217;s see the pom:</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  org.<span style="color: #006633;">sonatype</span>.<span style="color: #006633;">maven</span>.<span style="color: #006633;">plugin</span>
  emma4it<span style="color: #339933;">-</span>maven<span style="color: #339933;">-</span>plugin
  <span style="color: #cc66cc;">1.3</span>
&nbsp;
      instrument
&nbsp;
        instrument<span style="color: #339933;">-</span>project<span style="color: #339933;">-</span>artifact
&nbsp;
        <span style="color: #000066; font-weight: bold;">true</span></pre></div></div>


<p>Note that is using a different plugin: emma4it-maven-plugin.  This plugin provides some additional goals for instrumenting integrations tests.    For this project we use <strong>instrument-project-artifact</strong> goal. This goal will add Emma instrumentation into the project jar.  This is specific to tests running on integration-test phase, because it does requires that the packaged artifact be instrumented.</p>

<p>There is also the configuration <em>appendEmma</em>.  When true, this goal will shade the Emma jar into the instrumented jar.  This is very useful to test jars that will be launched from command line, like this project.</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">[EMMA v2.0.5312 report, generated Fri Jun 12 16:05:45 BRT 2009]
-------------------------------------------------------------------------------
OVERALL COVERAGE SUMMARY:
&nbsp;
[class, %]	[method, %]	[block, %]	[line, %]	[name]
100% (1/1)	100% (3/3)	89%  (16/18)	88%  (7/8)	all classes</pre></div></div>


<p>At this point we have a very decent code coverage, almost 90%, 7 of 8 lines of code, and on this project it is not possible to get 8/8.  And why is that? System.exit(). when this line is invoked nothing else is executed, that includes Emma instrumented code, so Emma won&#8217;t be able to know if System.exit() was executed, our integration tests catch that, because we check the exit code of the process, but Emma can&#8217;t do the same.</p>

<h3>Test Coverage Report</h3>

<p>At this point we have all coverage data stored at coverage.ec, in Emma binary format.  Let&#8217;s now convert that into something that is more readable.</p>

<p>Add the following execution on emma4it-maven-plugin:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  report
post-integration-test
&nbsp;
    report
&nbsp;
        ${project.build.sourceDirectory}</pre></div></div>


<p>Now the text, HTML, and XML reports are generated at target/site/emma.</p>

<div id="attachment_2481" class="wp-caption alignnone" style="width: 160px"><img class="size-thumbnail wp-image-2481" title="coveragereport" src="http://www.sonatype.com/people/wp-content/uploads/2009/06/coveragereport-150x150.png" alt="Emma HTML coverage report" width="150" height="150" /><p class="wp-caption-text">Emma HTML coverage report</p></div>

<p>That is it for part 2.  On the next part, I will show some ideas about testing more complex projects like <a href="http://flexmojos.sonatype.org/">flexmojos</a> and <a href="http://nexus.sonatype.org/">nexus</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integration tests with Maven (Part 1): Failsafe Plugin</title>
		<link>http://blog.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/</link>
		<comments>http://blog.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[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[<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://blog.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interview with Rich Seddon on m2eclipse Testing</title>
		<link>http://blog.sonatype.com/people/2009/02/interview-with-rich-seddon-on-m2eclipse-testing/</link>
		<comments>http://blog.sonatype.com/people/2009/02/interview-with-rich-seddon-on-m2eclipse-testing/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 13:00:55 +0000</pubDate>
		<dc:creator>Tim O'Brien</dc:creator>
				<category><![CDATA[m2eclipse]]></category>
		<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[OSGi]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=1628</guid>
		<description><![CDATA[In a previous post entitled m2e Roadmap, Jason discussed Sonatype&#8217;s commitment to quality and testing for m2eclipse. As a follow-up, I did a quick interview with Rich Seddon and asked him some questions about Sonatype&#8217;s approach to testing for Eclipse plugins. The interview is approximately five minutes long and in this interview, you&#8217;ll here Rich [...]]]></description>
				<content:encoded><![CDATA[<p>In a previous post entitled <a href="http://www.sonatype.com/people/2009/01/m2e-roadmap/">m2e Roadmap</a>, Jason discussed Sonatype&#8217;s commitment to quality and testing for m2eclipse.    As a follow-up, I did a quick interview with Rich Seddon and asked him some questions about Sonatype&#8217;s approach to testing for Eclipse plugins.     The interview is approximately five minutes long and in this interview, you&#8217;ll here Rich give some detailed descriptions of the tools he uses to test the m2eclipse plugin.</p>

<p>[media id=1 width=320 height=70]
<span id="more-1628"></span>
INTERVIEW TRANSCRIPT</p>

<p><b>Tim O’Brien:</b>  So, Rich could you introduce yourself?</p>

<p><b>Rich Seddon:</b>  Yeah, my name is Rich Seddon and I’ve been brought on at Sonatype for the purpose of producing a set of automated tests for the M2 Eclipse Product and the reason for doing this is that there’s a lot of performance improvements and bugs that we want to fix. But if we don’t have the automated test in place we won’t know if we’re breaking things. And it’s got to be automated because of the number of platforms that we support and the number of operating systems that we run on is very large and the manual testing process is really much, much more than a small company can possibly handle.</p>

<p><b>TO:</b>  So, can you give me a sense of what’s the difference between what you do and something like a basic  JUnit test.</p>

<p><b>RS:</b>  Well, what I’m actually doing here is a full set of system tests for M2 eclipse so I actually in the test harness bring up the M2 eclipse on various platforms and drive the UI through a set of scenarios and verify along the way that no errors have occurred and that the state of the UI is as expected and the state of the project under test is as expected&#8230;.you might say that what I’m doing is trying to capture, in an automated fashion, the types of usage that our users will actually go through from day to day.</p>

<p><b>TO:</b>  What kind of tools are you using for this?</p>

<p><b>RS:</b>    Right now I’m using uumm; I’m kind of using Hybrid approach. There are sort of two approaches to doing eclipse UI I testing. One of them is to drive it with a sort of UI robot where the robot pushes buttons and manipulates trees and things like that. And the other approach is sort of a white box testing approach where you bring up in a regular old unit test, eclipse, and you drive eclipse at the API level to the unit test.   Both of these tests have their advantages and disadvantages.</p>

<p>The reason that I got attracted to this position is that, in the last few years, it seems there’s been a few sort of hybrid approaches come along one of them is called SWT POD (sp?)    PPTP by eclipse and then there was Abbott.  Then there’s a commercial company, Instantiations has a product called “Window Tester”.  And these allow you to do both kinds of testing, UI Robot testing and API testing at the same time. What they do is they sort of record your actions as your drive eclipse through a set of scenarios and produce a JUnit test that you can then edit.</p>

<p><b>TO:</b>   Is this run as a part of the Maven build for m2eclipse?</p>

<p><b>RS:</b>   Yeah, it is run in…..  integration using the Maven OSGI plugin. There’s an OSGI test runner plug-in that actually brings up the eclipse, loads in the unit test, and then within the running eclipses project runs the unit test.</p>

<p><b>TO:</b>  So, what sorts of things have you found through the testing that we wouldn’t have found if we hadn’t been doing this?</p>

<p><b>RS:</b>  Well, I’ve found quite a few things. There’s been certainly some deadlock situations that I don’t think we would have been able to reproduce. We would have got reports of them, but we never would have been able to reproduce them in-house because they don’t happen that often. But, when you have these tests running continuously and you’re capturing the output of them when things go wrong then that allows us to find intermittent bugs which is very difficult to do otherwise with manual testing.</p>

<p>The other thing that I’m finding though is that because I’m actually driving eclipse through an automated scenario, it forces me to pay very close attention to eclipse itself. So, I actually think even  the manual testing that I’ve been doing has benefited from this process because I have to really think very carefully about how I’m driving eclipse through various scenarios in order to be able to produce these tests.</p>

<p><b>TO:</b>  I notice that sometimes the bugs that people find in a plug-in for eclipse have nothing to do with the plug-in itself. Are you testing different distributions of eclipse; like different combinations of plug-ins?</p>

<p><b>RS:</b>  I am not doing so much of that yet. I’ve just got started about a month ago, but it’s on the road map. So, I plan to have these automated tests&#8212; Right now they’re just running in Eclipse 3.4 and they’re running essentially with all the of the required plug-ins that M2Eclipse could ever never need: WTP, Subclipse and all kinds of things like that. But yes, it’s on the road map for maybe in a month or two, I’m going to start expanding the set of platforms and have different sets of plug-ins and different combinations of plug-ins. Certainly Eclipse 3.3, possibly Eclipse 3.2, IBM RAD and of course get running on different OS; These are all on the road map. And, once I’ve got the automated test running, I mean this is the advantage of the automation right, I can really easily expand it to be able to get good quality testing on all these different platforms.</p>

<p><b>TO:</b>  Well, thanks for taking the time to talk to us. And, we’ll check with you in a few months to see what progress you’ve made.</p>

<p><b>RS:</b>  All right, sounds good.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2009/02/interview-with-rich-seddon-on-m2eclipse-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
