<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6293455831777973426</id><updated>2012-02-27T10:54:06.509Z</updated><title type='text'>Agile Engineering Techniques</title><subtitle type='html'>Continuous Integration And Unit Testing With Maven
By John Dobie.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-8539279418450966297</id><published>2012-01-16T14:13:00.036Z</published><updated>2012-01-16T15:02:16.940Z</updated><title type='text'>Code Forensics</title><content type='html'>How do you know if using code metrics really does help to produce code with fewer bugs.&lt;br/&gt;I am convinced they do, but how can I possibly prove it?&lt;br /&gt;&lt;br /&gt;All projects have historic data.  This is usually stored in your bug tracking and source code control tools.&lt;br/&gt;We can use the data stored in these systems to perform ‘code forensics.’&lt;br/&gt;  We use the historic data from real issues to see if they could have been avoided.&lt;br /&gt;&lt;br /&gt;This can all be done without affecting any of your existing code or adding any risk to your project.  &lt;br/&gt;Surely that’s a useful Software Engineering technique?&lt;br /&gt;&lt;h2&gt;Disclaimer&lt;/h2&gt;Firstly I realise that most bugs you find in a standard project are not caused by code quality – it’s probably only a small percentage.  However the ones that exist are avoidable.  &lt;br/&gt;It is these avoidable quality issues that I want to concentrate on.  I want to be able to determine when exceeding a metric threshold is likely to result in a problem&lt;br/&gt;It’s possible that if enough code forensics are run on my individual code base, I may be able to come up with some numbers that are useful to me in the future.&lt;br/&gt;In the long term it may be possible for someone to do a large study and come up with better guidelines.&lt;br/&gt;&lt;h2&gt;Process&lt;/h2&gt;The process is quite straightforward.&lt;br /&gt;&lt;br /&gt;1. Query your bug tracking tool for all the issues that required a code fix.&lt;br /&gt;2. Assess the defects.&lt;br /&gt;3. Identify the code.&lt;br /&gt;4. Get the root cause.&lt;br /&gt;&lt;h2&gt;Query your bug tracking tool.&lt;/h2&gt;First thing you need to do is to identify all your recent bugs, let’s say for the last month.  &lt;br/&gt;Do a simple query to bring back all of the bugs during that period.This should be easy - otherwise you’re using the wrong tool! &lt;br /&gt;Now you have a full list of all of the defects that you are potentially interested in&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-eDapBJwUqyQ/TxBaivhTEfI/AAAAAAAAAHo/SmDjL_IXP74/s1600/jira.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="153" src="http://4.bp.blogspot.com/-eDapBJwUqyQ/TxBaivhTEfI/AAAAAAAAAHo/SmDjL_IXP74/s640/jira.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;h2&gt;Assess the defects.&lt;/h2&gt;You now need to go through each of the bugs and assess whether the issue really was a code issue.&lt;br/&gt;Other things it might be include.&lt;br /&gt;&lt;br /&gt;• A requirements issue.&lt;br /&gt;• An issue with the deployment environment.&lt;br /&gt;• Configuration Issues&lt;br /&gt;&lt;br/&gt;What you are left with is a list of issues that were really caused because of bad code. &lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-xKcTS25_4qM/TxBa11EjvzI/AAAAAAAAAHw/nqQiZC9oXgQ/s1600/code-error.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="56" src="http://3.bp.blogspot.com/-xKcTS25_4qM/TxBa11EjvzI/AAAAAAAAAHw/nqQiZC9oXgQ/s640/code-error.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;h2&gt;Identify the problematic code. &lt;/h2&gt;You now need to map your list of issues back to the relevant source.&lt;br /&gt;You will not be able to do this unless you have been disciplined with your check in comments.  In most places I have worked, when checking in a bug fix you always start it with a reference to the problem it fixes.  &lt;br /&gt;Assuming you have been commenting your commits with the reference,  you can do a simple query to see which code was affected.  This can be done in fisheye, tortoise etc to get the required code&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-Bo249xK_mIU/TxBbHomqokI/AAAAAAAAAH4/1JODPpjdPH0/s1600/fix.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="184" src="http://2.bp.blogspot.com/-Bo249xK_mIU/TxBbHomqokI/AAAAAAAAAH4/1JODPpjdPH0/s640/fix.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;h2&gt;Get to the root cause.&lt;/h2&gt;Finally you have something to look at, so what do you do with it?  Well first you have to understand how the fix works and decide if it was a code quality issue.  Perhaps the issue was a simple error rather than something a metric would have caught.&lt;br /&gt;However you might open the code and find like this.  The average complexity in our system is 10.  This piece of code has a complexity of 106!&lt;br/&gt;This was an accident waiting to happen!&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-E7LEU8SKCeY/TxBbcRYaTkI/AAAAAAAAAIA/AmGt8d_DfFk/s1600/code.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="324" src="http://3.bp.blogspot.com/-E7LEU8SKCeY/TxBbcRYaTkI/AAAAAAAAAIA/AmGt8d_DfFk/s640/code.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Clearly the bug would have been more likely to have been caught, had we failed the build because the code failed to meet expected quality standards.  This is a potentially avoidable error.&lt;br /&gt;&lt;h2&gt;Another Angle.&lt;/h2&gt;Another way to try and establish a link between poor code quality and defects is to take advantage of something such as the Sonar hotspot view to see the most complex classes in you system.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&amp;nbsp;&lt;a href="http://1.bp.blogspot.com/-jMcGCdq-xM0/TxBcF3WiqnI/AAAAAAAAAIQ/khdlLsgCRAg/s1600/sonar_hotspot.jpg" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"&gt;&lt;img border="0" height="122" src="http://1.bp.blogspot.com/-jMcGCdq-xM0/TxBcF3WiqnI/AAAAAAAAAIQ/khdlLsgCRAg/s320/sonar_hotspot.jpg" width="320" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can then work backwards and examine the history of those files to see if those classes are causing issues in your codebase. &lt;br /&gt;The trouble is that it is not that simple.  High complexity files, which are used infrequently, are less likely to cause you trouble than those which are more frequently used, but of a lower complexity.&lt;br /&gt;&lt;h2&gt;Automating the process.&lt;/h2&gt;For this to be any use it probably needs to be automated so that a large sample of data can be examined. Some tools already make this link between defects and the related fix source code. &lt;br/&gt;The next step is to pull that data back and run your metrics analysis on the files.  &lt;br /&gt;&lt;h2&gt;Summary&lt;/h2&gt;None of this is conclusive,  however I still think it's a useful technique.&lt;br/&gt;&lt;br/&gt;What it is most likely to prove is that you have had past problems which you could have avoided with metrics.  It should also give you an idea of which metrics to use.&lt;br /&gt;&lt;br/&gt;It's likely also to show that most problems are not caused by poor code quality,  but other factors instead.&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-8539279418450966297?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/8539279418450966297/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2012/01/code-forensics.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/8539279418450966297'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/8539279418450966297'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2012/01/code-forensics.html' title='Code Forensics'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-eDapBJwUqyQ/TxBaivhTEfI/AAAAAAAAAHo/SmDjL_IXP74/s72-c/jira.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-2896813945194994928</id><published>2012-01-16T11:09:00.004Z</published><updated>2012-01-16T12:48:43.824Z</updated><title type='text'>Unit Test Code Coverage With Jacoco</title><content type='html'>&lt;title&gt;Insert title here&lt;/title&gt;It’s easy to collect unit test code coverage because all of the common tools are geared up for it.&lt;br /&gt;This article will explain how you can add unit test coverage to your Maven application in 10 minutes.&lt;br&gt;We will use the excellent Jacoco code coverage library to show how easy it is.&lt;br /&gt;&lt;h2&gt;Examples&lt;/h2&gt;All of the examples come from this article.&lt;br /&gt;&lt;a href="http://johndobie.blogspot.com/2011/11/test-doubles-with-mockito.html"&gt;http://johndobie.blogspot.com/2011/11/test-doubles-with-mockito.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;You can check them out from here&lt;br /&gt;&lt;pre class="brush:java"&gt;svn co&lt;br /&gt;https://designbycontract.googlecode.com/svn/trunk/examples/testing/test-doubles&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;With Maven 3 installed,  you can run them with this command.&lt;br /&gt;&lt;pre class="brush:java"&gt;mvn clean package&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt;What is Jacoco&lt;/h2&gt;&lt;div&gt;Jacoco is a free code coverage library for Java.  &lt;a href="http://www.eclemma.org/jacoco/"&gt;http://www.eclemma.org/jacoco/&lt;/a&gt;&lt;/div&gt;I use it because it is very simple to add to all types of build including ANT and Maven, and it is also very simple to add to Java containers or a standalone JVM.&lt;br /&gt;&lt;h2&gt;How Does it Work?&lt;/h2&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-icRrq-avBn0/TxBVxEv9WjI/AAAAAAAAAHQ/94jp0T5Gv54/s1600/jacoco.jpg" /&gt;&lt;br /&gt;Jacoco uses the standard JVM Tool Interface. &lt;a href="http://java.sun.com/developer/technicalArticles/J2SE/jvm_ti/"&gt;http://java.sun.com/developer/technicalArticles/J2SE/jvm_ti/&lt;/a&gt; &lt;br /&gt;In simple terms you attach a Jacoco agent to a JVM when it starts. It was introduced in JDK 5 for monitoring and profiling JVMs and being able to dynamically modify Java classes as they're being loaded.&lt;br/&gt;Whenever a class is loaded Jacoco can instrument the class so it can see when the class is called and what lines are called.  That’s how it builds up the coverage statistics.  This is all done on the fly. &lt;br/&gt;By default the results file is created when the JVM terminates.&lt;br /&gt;You can also run the agent in server mode which allows you to trigger a dump of the results.&lt;br /&gt;&lt;h2&gt;How do you attach the agent to the JVM?&lt;/h2&gt;This is a very simple process.  You must specify where the jacoco jar is located and then you pass some parameters to define how the agent is to run.&lt;br /&gt;&lt;pre class="brush:xml"&gt;-javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;A typical run might look like this&lt;br /&gt;&lt;pre class="brush:xml"&gt;-javaagent:jacoco.jar=destfile=${sonar.jacoco.itReportPath},includes=com.dbc.*&lt;br /&gt;&lt;/pre&gt;A full reference is found here.&lt;a href="http://www.eclemma.org/jacoco/trunk/doc/agent.html"&gt;http://www.eclemma.org/jacoco/trunk/doc/agent.html&lt;/a&gt;   &lt;br /&gt;&lt;h2&gt;Jacoco Support For Maven&lt;/h2&gt;The docs for the maven plugin are defined here. &lt;a href="http://www.eclemma.org/jacoco/trunk/doc/maven.html"&gt;http://www.eclemma.org/jacoco/trunk/doc/maven.html&lt;/a&gt;&lt;br /&gt;First we need to add the plugin itself.&lt;br /&gt;&lt;pre class="brush:xml"&gt;&lt;plugin&gt;&lt;br /&gt;  &lt;groupid&gt;org.jacoco&lt;/groupid&gt;&lt;br /&gt;  &lt;artifactid&gt;jacoco-maven-plugin&lt;/artifactid&gt;&lt;br /&gt;  &lt;version&gt;0.5.5.201112152213&lt;/version&gt;&lt;br /&gt;&lt;/plugin&gt;&lt;br /&gt;&lt;/pre&gt;We can then define where the jacoco reports are output.&lt;br /&gt;&lt;pre class="brush:xml"&gt;&lt;configuration&gt;&lt;br /&gt;  &lt;destfile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/destfile&gt;&lt;br /&gt;  &lt;datafile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/datafile&gt;&lt;br /&gt;&lt;/configuration&gt;&lt;br /&gt;&lt;/pre&gt;Finally we need to define the following 2 executions to make the agent run before the tests are run and also to make sure that the jacoco report task is run when package is executed.&lt;br /&gt;&lt;pre class="brush:xml"&gt;&lt;executions&gt;&lt;br /&gt;  &lt;execution&gt;&lt;br /&gt;    &lt;id&gt;jacoco-initialize&lt;/id&gt;&lt;br /&gt;    &lt;goals&gt;&lt;br /&gt;      &lt;goal&gt;prepare-agent&lt;/goal&gt;&lt;br /&gt;    &lt;/goals&gt;&lt;br /&gt;  &lt;/execution&gt;&lt;br /&gt;  &lt;execution&gt;&lt;br /&gt;    &lt;id&gt;jacoco-site&lt;/id&gt;&lt;br /&gt;    &lt;phase&gt;package&lt;/phase&gt;&lt;br /&gt;    &lt;goals&gt;&lt;br /&gt;      &lt;goal&gt;report&lt;/goal&gt;&lt;br /&gt;    &lt;/goals&gt;&lt;br /&gt;  &lt;/execution&gt;&lt;br /&gt;&lt;/executions&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br/&gt;&lt;a name="pom"&gt;&lt;/a&gt;All this together is shown here&lt;pre class="brush:xml"&gt;&lt;br /&gt;&lt;plugins&gt;&lt;br /&gt; &lt;plugin&gt;&lt;br /&gt;  &lt;groupId&gt;org.jacoco&lt;/groupId&gt;&lt;br /&gt;  &lt;artifactId&gt;jacoco-maven-plugin&lt;/artifactId&gt;&lt;br /&gt;  &lt;version&gt;0.5.5.201112152213&lt;/version&gt;&lt;br /&gt;  &lt;configuration&gt;&lt;br /&gt;   &lt;destFile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/destFile&gt;&lt;br /&gt;   &lt;dataFile&gt;${basedir}/target/coverage-reports/jacoco-unit.exec&lt;/dataFile&gt;&lt;br /&gt;  &lt;/configuration&gt;&lt;br /&gt;  &lt;executions&gt;&lt;br /&gt;   &lt;execution&gt;&lt;br /&gt;    &lt;id&gt;jacoco-initialize&lt;/id&gt;&lt;br /&gt;    &lt;goals&gt;&lt;br /&gt;     &lt;goal&gt;prepare-agent&lt;/goal&gt;&lt;br /&gt;    &lt;/goals&gt;&lt;br /&gt;   &lt;/execution&gt;&lt;br /&gt;   &lt;execution&gt;&lt;br /&gt;    &lt;id&gt;jacoco-site&lt;/id&gt;&lt;br /&gt;    &lt;phase&gt;package&lt;/phase&gt;&lt;br /&gt;    &lt;goals&gt;&lt;br /&gt;     &lt;goal&gt;report&lt;/goal&gt;&lt;br /&gt;    &lt;/goals&gt;&lt;br /&gt;   &lt;/execution&gt;&lt;br /&gt;  &lt;/executions&gt;&lt;br /&gt; &lt;/plugin&gt;&lt;br /&gt; &lt;plugin&gt;&lt;br /&gt;  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&lt;br /&gt;  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;&lt;br /&gt;  &lt;configuration&gt;&lt;br /&gt;   &lt;source&gt;1.5&lt;/source&gt;&lt;br /&gt;   &lt;target&gt;1.5&lt;/target&gt;&lt;br /&gt;  &lt;/configuration&gt;&lt;br /&gt; &lt;/plugin&gt;&lt;br /&gt;&lt;/plugins&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To run the examples execute the following command.&lt;br /&gt;&lt;pre class="brush:xml"&gt;mvn clean package&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;Results.&lt;/h1&gt;The results are published in /target/site/jacoco.&lt;br /&gt;&lt;img border="0" height="148" src="http://3.bp.blogspot.com/-1tk_giJBIJQ/TxBXxYsv8cI/AAAAAAAAAHg/z54sLZwJ4R4/s640/results.jpg" width="640" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-2896813945194994928?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/2896813945194994928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2012/01/unit-test-code-coverage.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/2896813945194994928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/2896813945194994928'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2012/01/unit-test-code-coverage.html' title='Unit Test Code Coverage With Jacoco'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-icRrq-avBn0/TxBVxEv9WjI/AAAAAAAAAHQ/94jp0T5Gv54/s72-c/jacoco.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-8688923298142832355</id><published>2011-11-25T14:24:00.143Z</published><updated>2012-01-06T15:56:57.855Z</updated><title type='text'>Test Doubles With Mockito</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt;A common thing I come across is that teams using a mocking framework assume they are mocking.  &lt;br /&gt;They are not aware that Mocks are just one of a number of 'Test Doubles' which Gerard Meszaros has categorised at xunitpatterns.com.&lt;br /&gt;It’s important to realise that each type of test double has a different role to play in testing.  In the same way that you need to learn different patterns or refactoring’s, you need to understand the primitive roles of each type of test double.  These can then be combined to achieve your testing needs.&lt;br /&gt;I'll cover a very brief history of how this classification came about, and how each of the types differs.&lt;br /&gt;I'll do this using some short, simple examples in Mockito.&lt;br /&gt;&lt;h1&gt;A Very Brief History&lt;/h1&gt;For years people have been writing lightweight versions of system components to help with testing. In general it was called stubbing.  In 2000' the article 'Endo-Testing: Unit Testing with Mock Objects' introduced the concept of a Mock Object. Since then Stubs, Mocks and a number of other types of test objects have been classified by Meszaros as Test Doubles. &lt;br /&gt;This terminology has been referenced by Martin Fowler in "Mocks Aren't Stubs" and is being adopted within the Microsoft community as shown in "Exploring The Continuum of Test Doubles"&lt;br /&gt;A link to each of these important papers are shown in the reference section. &lt;br /&gt;&lt;h1&gt;Categories of test doubles&lt;/h1&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-qGqwaOnbSV0/TwbWfzr5tiI/AAAAAAAAAG0/GOggHOp7-6k/s1600/test_doubles.jpg" /&gt;The diagram above shows the commonly used types of test double. The following URL gives a good cross reference  to each of the patterns and their features as well as alternative terminology.&lt;br /&gt;&lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;http://xunitpatterns.com/Test%20Double.html&lt;/a&gt;&lt;br /&gt;&lt;h1&gt;Mockito&lt;/h1&gt;Mockito is a test spy framework and it is very simple to learn.  Notable with Mockito is that expectations of any mock objects are not defined before the test as they sometimes are in other mocking frameworks.  This leads to a more natural style(IMHO) when beginning mocking.&lt;br /&gt;The following examples are here purely to give a simple demonstration of using Mockito to implement the different types of test doubles.  &lt;br /&gt;There are a much larger number of specific examples of how to use Mockito on the website.&lt;br /&gt;&lt;a href="http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html"&gt;http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html&lt;/a&gt;&lt;br /&gt;&lt;h1&gt;Test Doubles with Mockito&lt;/h1&gt;Below are some basic examples using Mockito to show the role of each test double as defined by Meszaros. &lt;br /&gt;I’ve included a link to the main definition for each so you can get more examples and a complete definition.&lt;br /&gt;&lt;h1&gt;Dummy Object&lt;/h1&gt;&lt;a href="http://xunitpatterns.com/Dummy%20Object.html"&gt;http://xunitpatterns.com/Dummy%20Object.html&lt;/a&gt;&lt;br /&gt;This is the simplest of all of the test doubles.  This is an object that has no implementation which is used purely to populate arguments of method calls which are irrelevant to your test.&lt;br /&gt;For example, the code below uses a lot of code to create the customer which is not important to the test.&lt;br /&gt;The test couldn't care less which customer is added,  as long as the customer count comes back as one.&lt;br /&gt;&lt;pre class="brush:java"&gt;public Customer createDummyCustomer() {&lt;br /&gt; County county = new County("Essex");&lt;br /&gt; City city = new City("Romford", county);&lt;br /&gt; Address address = new Address("1234 Bank Street", city);&lt;br /&gt; Customer customer = new Customer("john", "dobie", address);&lt;br /&gt; return customer;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Test&lt;br /&gt;public void addCustomerTest() {&lt;br /&gt; Customer dummy = createDummyCustomer();&lt;br /&gt; AddressBook addressBook = new AddressBook();&lt;br /&gt; addressBook.addCustomer(dummy);&lt;br /&gt; assertEquals(1, addressBook.getNumberOfCustomers());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;We actually don't care about the contents of customer object - but it is required.  We can try a null value, but if the code is correct you would expect some kind of exception to be thrown.&lt;br /&gt;&lt;pre class="brush:java"&gt;@Test(expected=Exception.class)&lt;br /&gt;public void addNullCustomerTest() {&lt;br /&gt; Customer dummy = null;&lt;br /&gt; AddressBook addressBook = new AddressBook();&lt;br /&gt; addressBook.addCustomer(dummy);&lt;br /&gt;}  &lt;br /&gt;&lt;/pre&gt;To avoid this we can use a simple Mockito dummy to get the desired behaviour.&lt;br /&gt;&lt;pre class="brush:java"&gt;@Test&lt;br /&gt;public void addCustomerWithDummyTest() {&lt;br /&gt; Customer dummy = mock(Customer.class);&lt;br /&gt; AddressBook addressBook = new AddressBook();&lt;br /&gt; addressBook.addCustomer(dummy);&lt;br /&gt; Assert.assertEquals(1, addressBook.getNumberOfCustomers());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;It is this simple code which creates a dummy object to be passed into the call.&lt;br /&gt;&lt;pre class="brush:java"&gt;Customer dummy = mock(Customer.class);&lt;/pre&gt;Don't be fooled by the mock syntax - the role being played here is that of a dummy, not a mock.&lt;br /&gt;It's the role of the test double that sets it apart,  not the syntax used to create one. &lt;br /&gt;This class works as a simple substitute for the customer class and makes the test very easy to read.&lt;br /&gt;&lt;h1&gt;Test stub&lt;/h1&gt;&lt;a href="http://xunitpatterns.com/Test%20Stub.html"&gt;http://xunitpatterns.com/Test%20Stub.html&lt;/a&gt;&lt;br /&gt;The role of the test stub is to return controlled values to the object being tested. These are described as indirect inputs to the test. &amp;nbsp;Hopefully an example will clarify what this means.&lt;br /&gt;Take the following code&lt;br /&gt;&lt;pre class="brush:java"&gt;public class SimplePricingService implements PricingService&lt;br /&gt;{ &lt;br /&gt; PricingRepository repository;&lt;br /&gt;&lt;br /&gt; public SimplePricingService(PricingRepository pricingRepository) {&lt;br /&gt;  this.repository = pricingRepository;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Override&lt;br /&gt; public Price priceTrade(Trade trade) {&lt;br /&gt;  return repository.getPriceForTrade(trade);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Override&lt;br /&gt; public Price getTotalPriceForTrades(Collection&lt;trade&gt; trades) {&lt;br /&gt;  Price totalPrice = new Price();&lt;br /&gt;  for (Trade trade : trades)&lt;br /&gt;  {&lt;br /&gt;   Price tradePrice = repository.getPriceForTrade(trade);&lt;br /&gt;   totalPrice = totalPrice.add(tradePrice);&lt;br /&gt;  }&lt;br /&gt;  return totalPrice;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;The&amp;nbsp;SimplePricingService&amp;nbsp;has one collaborating object which is the trade repository.  The trade repository provides trade prices to the pricing service through the getPriceForTrade method. &lt;br /&gt;For us to test the businees logic in the SimplePricingService, we need to control these indirect inputs &lt;br /&gt;i.e. inputs we never passed into the test.&lt;br /&gt;This is shown below.&lt;br /&gt;&lt;img border="0" height="139" src="http://1.bp.blogspot.com/-f004NeMIiG4/TwcEUp05wVI/AAAAAAAAAHI/1UI_-Gd-gTY/s400/sequence.jpg" width="400" /&gt;&lt;br /&gt;In the following example we stub the PricingRepository to return known values which can be used to test the business logic of the SimpleTradeService.&lt;br /&gt;&lt;pre class="brush:java"&gt;@Test&lt;br /&gt;public void testGetHighestPricedTrade() throws Exception {&lt;br /&gt;  Price price1 = new Price(10); &lt;br /&gt;  Price price2 = new Price(15);&lt;br /&gt;  Price price3 = new Price(25);&lt;br /&gt; &lt;br /&gt;  PricingRepository pricingRepository = mock(PricingRepository.class);&lt;br /&gt;  when(pricingRepository.getPriceForTrade(any(Trade.class)))&lt;br /&gt;    .thenReturn(price1, price2, price3);&lt;br /&gt;   &lt;br /&gt;  PricingService service = new SimplePricingService(pricingRepository);&lt;br /&gt;  Price highestPrice = service.getHighestPricedTrade(getTrades());&lt;br /&gt;  &lt;br /&gt;  assertEquals(price3.getAmount(), highestPrice.getAmount());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt;Saboteur Example&lt;/h2&gt;There are 2 common variants of Test Stubs: Responder’s and Saboteur's.&lt;br /&gt;Responder's are used to test the happy path as in the previous example.&lt;br /&gt;A saboteur is used to test exceptional behaviour as below.&lt;br /&gt;&lt;pre class="brush:java"&gt;@Test(expected=TradeNotFoundException.class)&lt;br /&gt;public void testInvalidTrade() throws Exception {&lt;br /&gt;&lt;br /&gt;  Trade trade = new FixtureHelper().getTrade();&lt;br /&gt;  TradeRepository tradeRepository = mock(TradeRepository.class);&lt;br /&gt;&lt;br /&gt;  when(tradeRepository.getTradeById(anyLong()))&lt;br /&gt;    .thenThrow(new TradeNotFoundException());&lt;br /&gt;&lt;br /&gt;  TradingService tradingService = new SimpleTradingService(tradeRepository);&lt;br /&gt;  tradingService.getTradeById(trade.getId());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;Mock Object&lt;/h1&gt;&lt;a href="http://xunitpatterns.com/Mock%20Object.html"&gt;http://xunitpatterns.com/Mock%20Object.html&lt;/a&gt;&lt;br /&gt;Mock objects are used to verify object behaviour during a test. By object behaviour I mean we check that the correct methods and paths are excercised on the object when the test is run.&lt;br /&gt;This is very different to the supporting role of a stub which is used to provide results to whatever you are testing.&lt;br /&gt;In a stub we use the pattern of defining a return value for a method. &lt;br /&gt;&lt;pre class="brush:java"&gt;when(customer.getSurname()).thenReturn(surname);&lt;br /&gt;&lt;/pre&gt;In a mock we check the behaviour of the object using the following form.&lt;br /&gt;&lt;pre class="brush:java"&gt;verify(listMock).add(s);&lt;br /&gt;&lt;/pre&gt;Here is a simple example where we want to test that a new trade is audited correctly.&lt;br /&gt;Here is the main code.&lt;br /&gt;&lt;pre class="brush:java"&gt;public class SimpleTradingService implements TradingService{&lt;br /&gt;&lt;br /&gt;  TradeRepository tradeRepository;&lt;br /&gt;  AuditService auditService;&lt;br /&gt; &lt;br /&gt;  public SimpleTradingService(TradeRepository tradeRepository, &lt;br /&gt;                              AuditService auditService)&lt;br /&gt;  {&lt;br /&gt;    this.tradeRepository = tradeRepository;&lt;br /&gt;    this.auditService = auditService;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public Long createTrade(Trade trade) throws CreateTradeException {&lt;br /&gt;  Long id = tradeRepository.createTrade(trade);&lt;br /&gt;  auditService.logNewTrade(trade);&lt;br /&gt;  return id;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;The test below creates a stub for the trade repository and mock for the AuditService&lt;br /&gt;We then call verify on the mocked AuditService to make sure that the TradeService calls it's &lt;br /&gt;logNewTrade method correctly&lt;br /&gt;&lt;pre class="brush:java"&gt;@Mock&lt;br /&gt;TradeRepository tradeRepository;&lt;br /&gt; &lt;br /&gt;@Mock&lt;br /&gt;AuditService auditService;&lt;br /&gt;  &lt;br /&gt;@Test&lt;br /&gt;public void testAuditLogEntryMadeForNewTrade() throws Exception { &lt;br /&gt;  Trade trade = new Trade("Ref 1", "Description 1");&lt;br /&gt;  when(tradeRepository.createTrade(trade)).thenReturn(anyLong()); &lt;br /&gt;  &lt;br /&gt;  TradingService tradingService &lt;br /&gt;    = new SimpleTradingService(tradeRepository, auditService);&lt;br /&gt;  tradingService.createTrade(trade);&lt;br /&gt;  &lt;br /&gt;  verify(auditService).logNewTrade(trade);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;The following line does the checking on the mocked AuditService.&lt;br /&gt;&lt;pre class="brush:java"&gt;verify(auditService).logNewTrade(trade);&lt;/pre&gt;This test allows us to show that the audit service behaves correctly when creating a trade.&lt;br /&gt;&lt;h1&gt;Test Spy&lt;/h1&gt;&lt;a href="http://xunitpatterns.com/Test%20Spy.html"&gt;http://xunitpatterns.com/Test%20Spy.html&lt;/a&gt;&lt;br /&gt;It's worth having a look at the above link for the strict definition of a Test Spy.&lt;br /&gt;However in Mockito I like to use it to allow you to wrap a real object and then verify or modify it's behaviour to support your testing. &lt;br /&gt;Here is an example were we check the standard behaviour of a List.  Note that we can both verify that the add method is called and also assert that the item was added to the list.&lt;br /&gt;&lt;pre class="brush:java"&gt;@Spy&lt;br /&gt;List&lt;string&gt; listSpy = new ArrayList&lt;string&gt;();&lt;br /&gt;&lt;br /&gt;@Test&lt;br /&gt;public void testSpyReturnsRealValues() throws Exception {&lt;br /&gt; String s = "dobie";&lt;br /&gt; listSpy.add(new String(s));&lt;br /&gt;&lt;br /&gt; verify(listSpy).add(s);&lt;br /&gt; assertEquals(1, listSpy.size());&lt;br /&gt;}&lt;br /&gt;&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;Compare this with using a mock object where only the method call can be validated.  Because we only mock the behaviour of the list,  it does not record that the item has been added and returns the default value of zero when we call the size() method.&lt;br /&gt;&lt;pre class="brush:java"&gt;@Mock&lt;br /&gt;List&lt;string&gt; listMock = new ArrayList&lt;string&gt;();&lt;br /&gt;&lt;br /&gt;@Test&lt;br /&gt;public void testMockReturnsZero() throws Exception {&lt;br /&gt; String s = "dobie";&lt;br /&gt;&lt;br /&gt; listMock.add(new String(s));&lt;br /&gt;&lt;br /&gt; verify(listMock).add(s);&lt;br /&gt; assertEquals(0, listMock.size());&lt;br /&gt;}&lt;br /&gt;&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;Another useful feature of the testSpy is the ability to stub return calls.  When this is done the object will behave as normal until the stubbed method is called.  &lt;br /&gt;In this example we stub the get method to always throw a RuntimeException.  The rest of the behaviour remains the same. &lt;br /&gt;&lt;pre class="brush:java"&gt;@Test(expected=RuntimeException.class)&lt;br /&gt;public void testSpyReturnsStubbedValues() throws Exception {&lt;br /&gt; listSpy.add(new String("dobie"));  &lt;br /&gt; assertEquals(1, listSpy.size());&lt;br /&gt;  &lt;br /&gt; when(listSpy.get(anyInt())).thenThrow(new RuntimeException());&lt;br /&gt; listSpy.get(0);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;In this example we again keep the core behaviour but change the size() method to return 1 initially and 5 for all subsequent calls.&lt;br /&gt;&lt;pre class="brush:java"&gt;public void testSpyReturnsStubbedValues2() throws Exception {&lt;br /&gt; int size = 5;&lt;br /&gt; when(listSpy.size()).thenReturn(1, size);&lt;br /&gt;  &lt;br /&gt; int mockedListSize = listSpy.size();&lt;br /&gt; assertEquals(1, mockedListSize);&lt;br /&gt;  &lt;br /&gt; mockedListSize = listSpy.size();&lt;br /&gt; assertEquals(5, mockedListSize);  &lt;br /&gt;&lt;br /&gt; mockedListSize = listSpy.size();&lt;br /&gt; assertEquals(5, mockedListSize);  &lt;br /&gt;} &lt;br /&gt;&lt;/pre&gt;This is pretty Magic!&lt;br /&gt;&lt;h1&gt;Fake Object&lt;/h1&gt;&lt;a href="http://xunitpatterns.com/Fake%20Object.html"&gt;http://xunitpatterns.com/Fake%20Object.html&lt;/a&gt;&lt;br /&gt;Fake objects are usually hand crafted or light weight objects only used for testing and not suitable for production.  A good example would be an in-memory database or fake service layer.&lt;br /&gt;They tend to provide much more functionality than standard test doubles and as such are probably not usually candidates for implementation using Mockito. That’s not to say that they couldn’t be constructed as such,  just that its probably not worth implementing this way.&lt;br /&gt;&lt;h1&gt;References&lt;/h1&gt;&lt;a href="http://xunitpatterns.com/Test%20Double%20Patterns.html"&gt;Test Double Patterns&lt;/a&gt;&lt;br /&gt;&lt;a href="http://static.mockobjects.com/files/endotesting.pdf"&gt;Endo-Testing: Unit Testing with Mock Objects&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.jmock.org/oopsla2004.pdf"&gt;Mock Roles, Not Objects&lt;/a&gt;&lt;br /&gt;&lt;a href="http://martinfowler.com/articles/mocksArentStubs.html"&gt;Mocks Aren't Stubs&lt;/a&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163358.aspx"&gt;http://msdn.microsoft.com/en-us/magazine/cc163358.aspx&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-8688923298142832355?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/8688923298142832355/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2011/11/test-doubles-with-mockito.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/8688923298142832355'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/8688923298142832355'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2011/11/test-doubles-with-mockito.html' title='Test Doubles With Mockito'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-qGqwaOnbSV0/TwbWfzr5tiI/AAAAAAAAAG0/GOggHOp7-6k/s72-c/test_doubles.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-5788733056179465311</id><published>2011-10-17T14:20:00.013+01:00</published><updated>2011-11-25T12:27:07.282Z</updated><title type='text'>Maven Integration Testing And Spring Restful Services</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt;My original blog showed how to seperate maven unit and integration tests using a very simple example.&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html&lt;/a&gt;Since then a lot of people asked me for a more realistic example than the one used originally. This post shows how you split your unit and integration tests using the original method in a realistic environment where the application is actually deployed to a server.&lt;ul&gt;&lt;li&gt;We use Maven to build and unit test some Spring based restful webservices.&lt;/li&gt;&lt;li&gt;We then use the Maven Jetty plugin to start a Web server and deploy them to it.&lt;/li&gt;&lt;li&gt;We create an In-memory database and create the schema&amp;nbsp;&lt;/li&gt;&lt;li&gt;Finally we run all of the integration tests in the seperate  &lt;folder&gt;\src\integrationtest\java &lt;/folder&gt; directory&lt;/li&gt;&lt;/ul&gt;This article is aimed squarely at showing how to use Maven in a realistic way to start and deploy a set of services to a running server, before running your integration tests.  It is not about  the subtle details of REST or Spring MVC. I'll cover this lightly enough to build a working application whilst providing references to more in depth articles for those that want more details.&lt;h1&gt;Code Structure&lt;/h1&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;a href="http://2.bp.blogspot.com/-TfqtPz8ARko/TmdghEhdXqI/AAAAAAAAADs/hFmfmPM7A2w/s1600/rest-code-structure.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="400" src="http://2.bp.blogspot.com/-TfqtPz8ARko/TmdghEhdXqI/AAAAAAAAADs/hFmfmPM7A2w/s400/rest-code-structure.jpg" width="210" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h1&gt;Running the Example&lt;/h1&gt;The full code is hosted at google code.  Use the following commands to check it out and run it.Make sure you have nothing running on port 8080 before running the tests.&lt;pre class="brush:bash"&gt;svn co https://designbycontract.googlecode.com/svn/trunk/examples/maven/spring-rest-example&lt;br /&gt;cd spring-rest-example&lt;br /&gt;mvn clean install -Pit,jetty&lt;br /&gt;&lt;/pre&gt;You can see the full build on the following Cloudbees hosted Jenkins instance.&lt;a href="https://designbycontract.ci.cloudbees.com/job/spring-rest-example/"&gt;https://designbycontract.ci.cloudbees.com/job/spring-rest-example/&lt;/a&gt;&lt;h1&gt;Results of running the example&lt;/h1&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;a href="http://2.bp.blogspot.com/-MeBnZ1VDvOU/TleKJAjZRMI/AAAAAAAAADQ/KLAeWIPDWHs/s1600/jetty-deploy.jpg" imageanchor="1" style="clear: left; float: none; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="261" src="http://2.bp.blogspot.com/-MeBnZ1VDvOU/TleKJAjZRMI/AAAAAAAAADQ/KLAeWIPDWHs/s400/jetty-deploy.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;The tests in the standard maven test structure are run during the unit test phase as usual.&lt;/li&gt;&lt;li&gt;A Jetty Webserver is started &lt;/li&gt;&lt;li&gt;The war containing the web server is deployed to the server&lt;/li&gt;&lt;li&gt;The hsqldb in-memory database is started and the schema created.&lt;/li&gt;&lt;li&gt;The tests in the &lt;folder&gt;\src\integrationtest\java &lt;/folder&gt;directory are run during the integration test phase.&lt;/li&gt;&lt;li&gt;The server is shutdown.&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;How to create the Spring Service class&lt;/h1&gt;The trade service is very simple.  It uses a repository to create and find trades. I haven't included exceptions to keep the whole thing as simple as possible.The only trick here is to add the @Service annotation,  otherwise it is straight Java.&lt;pre class="brush:java"&gt;@Service&lt;br /&gt;public class SimpleTradeService implements TradeService {&lt;br /&gt;  @Autowired&lt;br /&gt;  TradeRepository tradeRepository; &lt;br /&gt; &lt;br /&gt;  public SimpleTradeService(TradeRepository tradeRepository)  {&lt;br /&gt;    this.tradeRepository = tradeRepository;&lt;br /&gt;  }&lt;br /&gt; &lt;br /&gt;  @Override&lt;br /&gt;  public Long createTrade(Trade t) {&lt;br /&gt;    Long id = tradeRepository.createTrade(t);&lt;br /&gt;    return id;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @Override&lt;br /&gt;  public Trade getTradeById(Long id) {&lt;br /&gt;    return tradeRepository.getTradeById(id);&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;How to create the Database repository class&lt;/h1&gt;The above service uses a trade repository to create and find trades.  We use the Spring  class HibernateDaoSupoort to create this class and keep things simple. By extending this class we simply need to create our trade object class, and define our database details in the spring config.  All of the other details are taken care of by the framework.&lt;pre class="brush:java"&gt;public class HibernateTradeRepository  extends HibernateDaoSupport implements TradeRepository{&lt;br /&gt;  @Override&lt;br /&gt;  public Trade getTradeByReference(String reference) {&lt;br /&gt;       throw new RuntimeException();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @Override&lt;br /&gt;  public Long createTrade(Trade trade) {&lt;br /&gt;      return (Long) getHibernateTemplate().save(trade);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @Override&lt;br /&gt;  public Trade getTradeById(Long id) {&lt;br /&gt;      return getHibernateTemplate().get(Trade.class, id);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;How to create the Database Trade Class&lt;/h1&gt;We use standard JPA annotations to define our database trade object&lt;pre class="brush:java"&gt;@Entity&lt;br /&gt;public class Trade {&lt;br /&gt; @Id&lt;br /&gt; private long id;&lt;br /&gt;&lt;/pre&gt;The @Entity annotation marks the object as a database entity.The @Id annotation shows which field we want to be our table primary key.For the rest of the fields we use default behaviour so no other annotations are required.&lt;h1&gt;How to Configure the Database&lt;/h1&gt;For this example we are going to use Hsqldb to create our database.&amp;nbsp;&lt;a href="http://hsqldb.org/"&gt;http://hsqldb.org/&lt;/a&gt;A new instance of this will be created every time we start the server.To setup the database all we have to do is define it in the spring config trade-servlet.xml&lt;pre class="brush:xml"&gt;&amp;lt;bean id="sessionFactory"   &lt;br /&gt;&amp;lt;bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&amp;gt;&lt;br /&gt;    &amp;lt;property name="packagesToScan"&lt;br /&gt;      value="com.dbc.model" /&amp;gt;&lt;br /&gt;    &amp;lt;property name="hibernateProperties"&amp;gt;&lt;br /&gt;      &amp;lt;props&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.show_sql"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.format_sql"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.transaction.factory_class"&amp;gt;&lt;br /&gt;          org.hibernate.transaction.JDBCTransactionFactory&lt;br /&gt;        &amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.dialect"&amp;gt;org.hibernate.dialect.HSQLDialect&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.connection.pool_size"&amp;gt;0&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.connection.driver_class"&amp;gt;org.hsqldb.jdbcDriver&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.connection.url"&amp;gt;&lt;br /&gt;          jdbc:hsqldb:target/data/tradedatabase;shutdown=true&lt;br /&gt;        &amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.connection.username"&amp;gt;sa&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.connection.password"&amp;gt;&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.connection.autocommit"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.jdbc.batch_size"&amp;gt;0&amp;lt;/prop&amp;gt;&lt;br /&gt;        &amp;lt;prop key="hibernate.hbm2ddl.auto"&amp;gt;update&amp;lt;/prop&amp;gt;&lt;br /&gt;      &amp;lt;/props&amp;gt;&lt;br /&gt;    &amp;lt;/property&amp;gt;&lt;br /&gt;  &amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;/pre&gt;The session factory defines our database connection details.  The most important property is &lt;pre class="brush:xml"&gt;&amp;lt;prop key="hibernate.hbm2ddl.auto"&amp;gt;update&amp;lt;/prop&amp;gt;&lt;br /&gt;&lt;/pre&gt;This property tells hibernate to update the database when the application starts. It will effectively create the table for the trade object from the annotations on our trade object.When you run the tests, you will see that the following SQL is executed on startup.&lt;pre class="brush:bash"&gt;11:30:31,899 DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate SchemaUpdate:203 &lt;br /&gt;- create table &lt;br /&gt;Trade (id bigint          not null, &lt;br /&gt;       description        varchar(255), &lt;br /&gt;       reference          varchar(255), &lt;br /&gt;       primary key (id))&lt;br /&gt;&lt;/pre&gt;Thats a new database setup and ready to go.&lt;h1&gt;Creating The Restful Interface.&lt;/h1&gt;I'm just going to cover the basics here. For some great examples follow these links&lt;a href="http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/"&gt;http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/&lt;/a&gt;&lt;a href="http://www.stupidjavatricks.com/?p=54"&gt;http://www.stupidjavatricks.com/?p=54&lt;/a&gt;&lt;h1&gt;How to Create the Spring Controller&lt;/h1&gt;The Spring controller is the key to this whole example. It is the controller that takes our requests and passes them to the trade Service for processing.  It defines the restful interface.We use @PathVariable to make things simple.&lt;pre class="brush:java"&gt;@RequestMapping(value = "/create/trade/{id}")&lt;br /&gt;public ModelAndView createTrade(@PathVariable Long id) {&lt;br /&gt;  Trade trade = new Trade(id); &lt;br /&gt;  service.createTrade(trade);&lt;br /&gt;  ModelAndView mav = new ModelAndView("tradeView", BindingResult.MODEL_KEY_PREFIX + "trade", trade);&lt;br /&gt;  return mav;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@RequestMapping(value = "/find/trade/{id}")&lt;br /&gt;public ModelAndView findTradeById(@PathVariable Long id) {&lt;br /&gt;  Trade trade = service.getTradeById(id);&lt;br /&gt;  ModelAndView mav = new ModelAndView("tradeView", BindingResult.MODEL_KEY_PREFIX + "trade", trade);&lt;br /&gt;  return mav;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;It works quite simply by populating the &lt;folder&gt;@PathVariable id&lt;/folder&gt; with the value from &lt;folder&gt;/find/trade/{id}&lt;/folder&gt;For example,  requesting /find/trade/1 will populate reference with "1"requesting /find/trade/29 will populate reference with "29"More information can be found here:&lt;a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates"&gt;http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates&lt;/a&gt;&lt;h1&gt;How to configure the Web Application&lt;/h1&gt;The configuration of the web application in web.xml is very straightforward.First we register the Spring Servlet&lt;pre class="brush:xml"&gt;&lt;servlet&gt;&lt;br /&gt;  &lt;servlet-name&gt;trade&lt;/servlet-name&gt;&lt;br /&gt;  &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;&lt;br /&gt;  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;&lt;br /&gt;&lt;/servlet&gt;&lt;/pre&gt;Next we define a mapping to the servlet.  This mapping will pass all requests to our servlet.&lt;pre class="brush:xml"&gt;&lt;servlet-mapping&gt;&lt;br /&gt;  &lt;servlet-name&gt;trade&lt;/servlet-name&gt;&lt;br /&gt;  &lt;url-pattern&gt;/*&lt;/url-pattern&gt;&lt;br /&gt;&lt;/servlet-mapping&gt;&lt;/pre&gt;&lt;h1&gt;How to Configure Spring&lt;/h1&gt;The Spring configuration consists of a number of distinct elements.The first line simply tells Spring where to look for annotations&lt;pre class="brush:xml"&gt;&lt;context:component-scan base-package="com.dbc"&gt;&lt;/context:component-scan&gt;&lt;/pre&gt;The BeanNameViewResolver takes the name &lt;pre class="brush:xml"&gt;&lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver"&gt;&lt;/bean&gt;&lt;/pre&gt;This scary looking piece of XML does the job of making sure that the Trade object is returned as XML.XStream will take the object and automatically convert to an XML Format.&lt;pre class="brush:xml"&gt;&lt;bean class="org.springframework.web.servlet.view.xml.MarshallingView" id="tradeView"&gt;&lt;br /&gt;  &lt;constructor-arg&gt;&lt;bean class="org.springframework.oxm.xstream.XStreamMarshaller"&gt;&lt;br /&gt;      &lt;property name="autodetectAnnotations" value="true"&gt;&lt;br /&gt;    &lt;/property&gt;&lt;/bean&gt;&lt;br /&gt;  &lt;/constructor-arg&gt;&lt;/bean&gt;    &lt;/pre&gt;The Trade class defines the XStream annotation for this.&lt;pre class="brush:java"&gt;@XStreamAlias("trade")&lt;br /&gt;public class Trade {&lt;br /&gt;&lt;/pre&gt;In our case you can see from the test that we get the following from /search/trade/1&lt;pre class="brush:xml"&gt;&lt;trade&gt;&lt;br /&gt;  &lt;id&gt;1&lt;/id&gt;&lt;br /&gt;&lt;/trade&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;How to start and stop the Jetty Server&lt;/h1&gt;I use the Jetty Plugin to start the server and deploy the war file contained the services.&lt;a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin"&gt;http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin&lt;/a&gt;The server is started with the following snippet from pom.xml&lt;pre class="brush:xml"&gt;&amp;lt;execution&amp;gt;&lt;br /&gt;  &amp;lt;id&amp;gt;start-jetty&amp;lt;/id&amp;gt;&lt;br /&gt;  &amp;lt;phase&amp;gt;pre-integration-test&amp;lt;/phase&amp;gt;&lt;br /&gt;  &amp;lt;goals&amp;gt;&lt;br /&gt;    &amp;lt;goal&amp;gt;run&amp;lt;/goal&amp;gt;&lt;br /&gt;  &amp;lt;/goals&amp;gt;&lt;br /&gt;&amp;lt;/execution&amp;gt;&lt;br /&gt;&lt;/pre&gt;The server is stoped with the following snippet from pom.xml&lt;pre class="brush:xml"&gt;&amp;lt;execution&amp;gt;&lt;br /&gt;  &amp;lt;id&amp;gt;stop-jetty&amp;lt;/id&amp;gt;&lt;br /&gt;  &amp;lt;phase&amp;gt;post-integration-test&amp;lt;/phase&amp;gt;&lt;br /&gt;  &amp;lt;goals&amp;gt;&lt;br /&gt;    &amp;lt;goal&amp;gt;stop&amp;lt;/goal&amp;gt;&lt;br /&gt;  &amp;lt;/goals&amp;gt;&lt;br /&gt;&amp;lt;/execution&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;How to run the Integration Tests&lt;/h1&gt;The integration tests are run using failsafe as described in the orgiinal article.&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html&lt;/a&gt;We use the new Spring RestTemplate to make the call to the service easy.&lt;pre class="brush:java"&gt;@Test&lt;br /&gt;public void testGetTradeFromRestService() throws Exception {&lt;br /&gt;  long id = 10L;&lt;br /&gt;  createTrade(id);&lt;br /&gt;  String tradeXml = new RestTemplate()&lt;br /&gt;                          .getForObject(&lt;br /&gt;                          "http://localhost:8080/find/trade/{id}",&lt;br /&gt;                          String.class, id);&lt;br /&gt;  &lt;br /&gt;  System.out.println(tradeXml);&lt;br /&gt;  Trade trade = getTradeFromXml(tradeXml);&lt;br /&gt;  assertEquals(trade.getId(), id);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-5788733056179465311?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/5788733056179465311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2011/10/maven-integration-testing-and-spring.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/5788733056179465311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/5788733056179465311'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2011/10/maven-integration-testing-and-spring.html' title='Maven Integration Testing And Spring Restful Services'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-TfqtPz8ARko/TmdghEhdXqI/AAAAAAAAADs/hFmfmPM7A2w/s72-c/rest-code-structure.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-116974524887743235</id><published>2011-08-03T17:07:00.259+01:00</published><updated>2011-10-19T13:51:41.085+01:00</updated><title type='text'>A Free EC2 Cloud Based Jenkins And Sonar Setup</title><content type='html'>&lt;h1&gt;Introduction&lt;/h1&gt;My original blog showed how to seperate maven unit and integration tests when doing continuous integration.&lt;br /&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;This example builds on this to provide a platform to run the example. &amp;nbsp;We use a free Amazon EC2 cloud based solution to show how to deploy Jenkins and Sonar. &amp;nbsp;This is a low power server but it is useful for infrequent use.&lt;br /&gt;&lt;br /&gt;The steps are very simple&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Sign up for a 1 year free account from Amazon&lt;/li&gt;&lt;li&gt;Create a new server from an existing image with Hudson and Sonar.&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;Viewing The Final Platform&lt;/h1&gt;You can see an example of the finished platform by clicking on the links below.&lt;br /&gt;&lt;br /&gt;Jenkins :&amp;nbsp;&lt;a href="http://ec2-75-101-221-43.compute-1.amazonaws.com:8080/"&gt;http://ec2-75-101-221-43.compute-1.amazonaws.com:8080/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Sonar   : &amp;nbsp;&amp;nbsp;&lt;a href="http://ec2-75-101-221-43.compute-1.amazonaws.com:9000/"&gt;http://ec2-75-101-221-43.compute-1.amazonaws.com:9000/&lt;/a&gt;&lt;br /&gt;&lt;h1&gt;Creating A Free Amazon Account&lt;/h1&gt;Amazon offer a free account for new customers.  First sign up for the account at the following link.&lt;br /&gt;&lt;a href="http://aws.amazon.com/free/"&gt;http://aws.amazon.com/free/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;Creating A Free Server&lt;/h1&gt;The free account only allows you to use a restricted set of images and a micro server.&lt;br /&gt;Follow the steps below to create the server.&lt;br /&gt;&lt;br /&gt;The first step is to log into the main Amazon console. &amp;nbsp;&lt;a href="https://console.aws.amazon.com/s3/home"&gt;https://console.aws.amazon.com/s3/home&lt;/a&gt;&lt;br /&gt;Go to the EC2 Tab and Click 'Launch Instance'&lt;br /&gt;&lt;br /&gt;&lt;img border="0" height="408" src="http://4.bp.blogspot.com/-vK-FotaziDc/TpMWtoY_FsI/AAAAAAAAAEk/krmdM5nLQvI/s640/laucnh-instance.jpg" width="640" /&gt;&lt;br /&gt;&lt;br /&gt;Next we have to choose the correct image.&amp;nbsp; Go to the community tab and look for the following image.&lt;br /&gt;ami-5d5f9234&lt;br /&gt;&lt;br /&gt;&lt;img border="0" height="414" src="http://1.bp.blogspot.com/-KNl8CNdtzkM/TpMWvcCo-UI/AAAAAAAAAEw/kcHkR-BQ9Wk/s640/image.jpg" width="640" /&gt;&lt;br /&gt;&amp;nbsp;Notice the 'Star' which shows it is free tier eligible if chosen with a Micro Instance.&lt;br /&gt;&lt;br /&gt;This brings up the screen below.&amp;nbsp; Notice the type 'Micro (t1.micro, 613MB).&lt;br /&gt;Leave the defaults and click on 'Continue' &lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/-x0fNMynlkBo/Tjlw3AA-RKI/AAAAAAAAAC0/eBGo3Op8Ons/s1600/instances-wizard.jpg" imageanchor="1" style="clear: left; float: none; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="436" src="http://2.bp.blogspot.com/-x0fNMynlkBo/Tjlw3AA-RKI/AAAAAAAAAC0/eBGo3Op8Ons/s640/instances-wizard.jpg" width="640" /&gt;&lt;/a&gt;Again leave the defaults and click on continue.&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/-x0fNMynlkBo/Tjlw3AA-RKI/AAAAAAAAAC0/eBGo3Op8Ons/s1600/instances-wizard.jpg" imageanchor="1" style="clear: left; float: none; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;img border="0" height="436" src="http://3.bp.blogspot.com/-Vnq5B3IQpOQ/TjlwyF9lTLI/AAAAAAAAACw/YzjOBLGTFE4/s640/instance-details.jpg" width="640" /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/-WI5rII7Wqlc/TjlwxT3VBFI/AAAAAAAAACs/cyk-gmOOOmE/s1600/keys.jpg" imageanchor="1" style="clear: left; float: none; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Once again leave the defaults and click on continue.&lt;img border="0" height="437" src="http://4.bp.blogspot.com/-WI5rII7Wqlc/TjlwxT3VBFI/AAAAAAAAACs/cyk-gmOOOmE/s640/keys.jpg" width="640" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Create Key Pair&lt;/h2&gt;&lt;img border="0" height="434" src="http://1.bp.blogspot.com/-cyYZ3idj8PQ/TpMIKMJqXEI/AAAAAAAAAEY/4YDp__6NhCs/s640/keys.jpg" width="640" /&gt;&lt;br /&gt;This key pair is important because it is needed to connect to your server. Click on the 'Create &amp;amp; Download your key pair'.&amp;nbsp; Keep the file safe.&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;Creating A Security Group&lt;/h1&gt;&lt;h1&gt;&lt;span style="font-size: small; font-weight: normal;"&gt;The next screen allows you to define the firewall rules for your server.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;&lt;img border="0" height="436" src="http://4.bp.blogspot.com/-yVhZ5ro-GNs/TpMJ0uNhomI/AAAAAAAAAEg/2stGXsdUFj0/s640/firewall-choices.jpg" width="640" /&gt;&lt;br /&gt;&lt;h1&gt;&lt;span style="font-size: small; font-weight: normal;"&gt;&amp;nbsp;We are going to allow all of the following rules below.&amp;nbsp; You can make it more restrictive if you like once everything is running.&lt;/span&gt;&lt;/h1&gt;&lt;br /&gt;&lt;img border="0" height="300" src="http://4.bp.blogspot.com/-R5FVWdgtF5w/TpMI7nmGXZI/AAAAAAAAAEc/lECPyukvyqc/s640/security-group.jpg" width="640" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;Instance Details.&lt;/h1&gt;&lt;h1&gt;&lt;span style="font-size: small; font-weight: normal;"&gt;The details of your new server will then be summarised.&amp;nbsp; &lt;/span&gt;&lt;/h1&gt;&lt;br /&gt;Click &lt;img border="0" height="436" src="http://4.bp.blogspot.com/-bRRqRaNRX7g/TpMWusGOLlI/AAAAAAAAAEs/nY5F8co1jpg/s640/summary.jpg" width="640" /&gt;&lt;br /&gt;&lt;h1&gt;&lt;span style="font-size: small; font-weight: normal;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;&lt;h1&gt;&lt;span style="font-size: small; font-weight: normal;"&gt;Click on 'Launch' to start the server.&lt;/span&gt;&lt;/h1&gt;&lt;h1&gt;&lt;span style="font-weight: normal;"&gt; &lt;/span&gt;&lt;/h1&gt;&lt;br /&gt;&lt;img border="0" height="322" src="http://4.bp.blogspot.com/-yj5Seaw4qlk/TpMWucsYzuI/AAAAAAAAAEo/SS2VCi9JITg/s640/launch.jpg" width="640" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;Tools to connect to your Server&lt;/h1&gt;You will need to connect to your server to install the applications.  To do this download and install putty.&lt;br /&gt;http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html &lt;br /&gt;&lt;br /&gt;This will give you all the tools you need to connect to the server.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Converting Your Security Key&lt;/h2&gt;Before you can use putty to login you need to convert the .pem key from step 1 to a .ppk key.&lt;br /&gt;Go to your installation of putty and run puttygen.&lt;br /&gt;Select load and choose your downloaded .pem file. &amp;nbsp;You should see the dialog below.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/-EcGbtl6Gxs8/Tosnd1Y_MLI/AAAAAAAAAEE/i69k4Oc8L0E/s1600/keygen.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="393" src="http://4.bp.blogspot.com/-EcGbtl6Gxs8/Tosnd1Y_MLI/AAAAAAAAAEE/i69k4Oc8L0E/s400/keygen.jpg" width="400" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Next click on 'Save Private key' and save the file somewhere safe.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Connecting To your Server&lt;/h2&gt;run putty&lt;br /&gt;- First fill in the in the server name&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/-98Y0uy7GuAQ/Tosn9xNhiBI/AAAAAAAAAEI/yd8LqUAm1Jk/s1600/putty-auth.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="310" src="http://1.bp.blogspot.com/-98Y0uy7GuAQ/Tosn9xNhiBI/AAAAAAAAAEI/yd8LqUAm1Jk/s320/putty-auth.jpg" width="320" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Next click on the SSH -&amp;gt; Auth property. &amp;nbsp;Browse to the new .ppk file and select it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/-ohI0f327A2s/Tosn_9TCswI/AAAAAAAAAEM/rc2VpPS-_JQ/s1600/putty-connect.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="310" src="http://2.bp.blogspot.com/-ohI0f327A2s/Tosn_9TCswI/AAAAAAAAAEM/rc2VpPS-_JQ/s320/putty-connect.jpg" width="320" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Then click open.&lt;br /&gt;This should give you a terminal window as follows showing you connected to your ec2 instance.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Installing Java&lt;/h2&gt;&lt;br /&gt;&lt;h2&gt;Installing Unzip&lt;/h2&gt;&lt;br /&gt;&lt;h2&gt;Installing Jenkins&lt;/h2&gt;We use the following commands to install Jenkins.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Installing Hudson&lt;/h2&gt;TBD&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-116974524887743235?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/116974524887743235/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2011/08/free-cloud-based-cit-environment-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/116974524887743235'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/116974524887743235'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2011/08/free-cloud-based-cit-environment-with.html' title='A Free EC2 Cloud Based Jenkins And Sonar Setup'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-vK-FotaziDc/TpMWtoY_FsI/AAAAAAAAAEk/krmdM5nLQvI/s72-c/laucnh-instance.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-3853309790822805227</id><published>2011-06-20T17:32:00.000+01:00</published><updated>2011-06-20T17:32:39.062+01:00</updated><title type='text'>Maven Multi-Module With Sonar and Jacoco</title><content type='html'>&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1. Introduction&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;This blog shows how to seperate your unit and integration tests in a multi-module maven project whilst providing&amp;nbsp;seperate test coverage results for each.&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It builds upon my previous 2 simpler examples using Sonar and Jacoco.&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html&lt;/a&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;It's a full example in its own right but I won't go into too much detail on how it works which is already done.&lt;/div&gt;&lt;div&gt;The previous examples cover everything in sufficient detail.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The full example can be checked out from google code and run as below.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1.1 Example Structure&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;We mirror the 2 structures as per the previous blogs with the unit and integration tests seperated.&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-F82XGF1ySsA/Tf9lSCM9YdI/AAAAAAAAAB8/t_WsXYSMAJU/s1600/multi+module.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-F82XGF1ySsA/Tf9lSCM9YdI/AAAAAAAAAB8/t_WsXYSMAJU/s1600/multi+module.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;For this example we also create a new directory for the jacoco files.&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;a href="http://2.bp.blogspot.com/-0xhUl-UqlMU/Tf9nOxSfL3I/AAAAAAAAACA/cWKVxQXxibY/s1600/multi-coverage.jpg" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-0xhUl-UqlMU/Tf9nOxSfL3I/AAAAAAAAACA/cWKVxQXxibY/s1600/multi-coverage.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;Because this is a multi-module example, &amp;nbsp;we have also have the top level pom.xml and common-pom.xml.&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1.2  Running the Example&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;The full code is hosted at google code. &amp;nbsp;Use the following commands to check it out and run it.&lt;/div&gt;&lt;pre style="background-color: #eeeeee; border-bottom-color: rgb(153, 153, 153); border-bottom-style: dashed; border-bottom-width: 1px; border-left-color: rgb(153, 153, 153); border-left-style: dashed; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: dashed; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: dashed; border-top-width: 1px; color: black; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 659px;"&gt;&lt;code&gt;svn co https://designbycontract.googlecode.com/svn/trunk/multi-module-sonar&lt;br /&gt;cd multi-module-sonar&lt;br /&gt;mvn –Pit,sonar clean sonar&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1.3 &amp;nbsp;Results of running the example&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;The tests in the standard maven test structure are run during the unit test phase as usual.&lt;/li&gt;&lt;li&gt;The tests in the&amp;nbsp;&lt;span class="Apple-style-span" style="color: #6fa8dc;"&gt;\src\integrationtest\java&amp;nbsp;&lt;/span&gt;directory are run during the integration test phase.&lt;/li&gt;&lt;li&gt;The integration test classes are placed in the seperate&amp;nbsp;&lt;span class="Apple-style-span" style="color: #6fa8dc;"&gt;\target\integrationtest-classes&lt;/span&gt;&amp;nbsp;directory&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-gxkxOie7OHI/Tf90O-IsUgI/AAAAAAAAACI/3SKS9DcvEFg/s1600/module1-output.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-gxkxOie7OHI/Tf90O-IsUgI/AAAAAAAAACI/3SKS9DcvEFg/s1600/module1-output.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;The aggregated jacoco coverage files are placed in the code-coverage directory&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-AjnB35Gqe_E/Tf90sSvekBI/AAAAAAAAACM/Wh3qHYOFUk0/s1600/jacoco-results.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-AjnB35Gqe_E/Tf90sSvekBI/AAAAAAAAACM/Wh3qHYOFUk0/s1600/jacoco-results.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The results are show in Sonar as an aggregation of the results for module1 and module2&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-iLTovSLhyWE/Tf9u7B5AR1I/AAAAAAAAACE/kNDw1_vllPQ/s1600/multi-sonar.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-iLTovSLhyWE/Tf9u7B5AR1I/AAAAAAAAACE/kNDw1_vllPQ/s1600/multi-sonar.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;2. How does it work&lt;/span&gt;&lt;div&gt;Check out the previous blogs to see how the unit and integration tests are run seperately and the code coverage works. &amp;nbsp;This example just builds upon that by using more than one module.&lt;/div&gt;&lt;div&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-3853309790822805227?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/3853309790822805227/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2011/06/maven-multi-module-with-sonar-and.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/3853309790822805227'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/3853309790822805227'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2011/06/maven-multi-module-with-sonar-and.html' title='Maven Multi-Module With Sonar and Jacoco'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-F82XGF1ySsA/Tf9lSCM9YdI/AAAAAAAAAB8/t_WsXYSMAJU/s72-c/multi+module.jpg' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-2648347740699454566</id><published>2011-06-03T12:38:00.105+01:00</published><updated>2011-10-12T16:28:27.387+01:00</updated><title type='text'>Separating Code Coverage With Maven, Sonar and Jacoco</title><content type='html'>&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1. Introduction&lt;/span&gt;&lt;br /&gt;In this example I will expand upon my previous example of keeping your unit and integration tests in separate packages, and explain how to also produce code coverage statistics.&lt;br /&gt;&lt;br /&gt;For this I continue to use Maven, whilst adding Sonar and Jacoco to provide the code coverage.&lt;br /&gt;You can run the example below, but you will need to read the first article here to understand it.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html"&gt;&lt;span class="Apple-style-span" style="color: #6fa8dc;"&gt;http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1.1 Example Structure&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here we have the typical maven structure and our new folder&amp;nbsp;&lt;span class="Apple-style-span" style="color: #6fa8dc;"&gt;\src\integrationtest\java&lt;/span&gt;&lt;br /&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-W7s50lNbfJw/Tei-n4nfKqI/AAAAAAAAABE/5vz45RWpnr8/s1600/sonar.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1.2 Running the Example&lt;/span&gt;&lt;br /&gt;The full code is hosted at google code. &amp;nbsp;Use the following commands to check it out and run it.&lt;br /&gt;&lt;pre class="brush:bash"&gt;svn co https://designbycontract.googlecode.com/svn/trunk/sonar&lt;br /&gt;cd sonar&lt;br /&gt;mvn –Psonar,it clean install sonar:sonar&lt;br /&gt;&lt;/pre&gt;You need to have sonar running. &amp;nbsp;Instructions on the install can be found here.&lt;br /&gt;&lt;a href="http://docs.codehaus.org/display/SONAR/Install+Sonar"&gt;http://docs.codehaus.org/display/SONAR/Install+Sonar&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,FreeSans,sans-serif; font-size: 13px; line-height: 17px;"&gt;Go to sonar web interface and click on 'configure widgets' to add the IT coverage widget&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,FreeSans,sans-serif; font-size: 13px; line-height: 17px;"&gt;You will also have to add the Jacoco plugin.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You can see the Jenkins build here:&lt;br /&gt;&lt;a href="http://ec2-75-101-221-43.compute-1.amazonaws.com:8080/job/maven-sonar-jaccoco/"&gt;http://ec2-75-101-221-43.compute-1.amazonaws.com:8080/job/maven-sonar-jaccoco/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;You can see the Sonar build here&lt;br /&gt;&lt;a href="http://ec2-75-101-221-43.compute-1.amazonaws.com:9000/dashboard/index/1"&gt;http://ec2-75-101-221-43.compute-1.amazonaws.com:9000/dashboard/index/1&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;1.3 &amp;nbsp;Results of running the example&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The tests in the standard maven test structure are run during the unit test phase as usual.&lt;/li&gt;&lt;li&gt;The tests in the integrationtest directory are run during the integration test phase.&lt;/li&gt;&lt;li&gt;The test classes and results are placed in a seperate integrationtest directory in target&lt;/li&gt;&lt;/ul&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-_knVuYHlss0/Tejv_66X1II/AAAAAAAAABg/JEJMfHyjXOw/s1600/test-classes.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;The jacoco test coverage reports are placed in the /target/coverage-reports directory&lt;br /&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-9QvY7D-nYYE/TpWttQRss9I/AAAAAAAAAFM/S5hLpLm2r8Q/s1600/reports.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;In Sonar, the unit test coverage is shown in one widget and the integration test coverage in another.&lt;br /&gt;&lt;br /&gt;&lt;img border="0" height="228" src="http://4.bp.blogspot.com/-PictPHdgbpA/TpWm_er-6qI/AAAAAAAAAFE/YnYXmhwey88/s320/sonar+coverage.jpg" width="320" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;2.0 How does it work?&lt;/span&gt;&lt;br /&gt;The basic steps are as follows. The details for each are below.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Run the maven integration-test lifecycle with Jacoco coverage enabled for unit and integration tests.&lt;/li&gt;&lt;li&gt;Run Sonar pointing to the coverage results.&lt;/li&gt;&lt;li&gt;Put it all in a profile so it can be used conveniently.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;2.1 &amp;nbsp;Running&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;the tests with Jacoco&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;We first define the directories for the jacoco coverage files.&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;coverage.reports.dir&amp;gt;&lt;br /&gt;  ${basedir}/target/coverage-reports&lt;br /&gt;&amp;lt;/coverage.reports.dir&amp;gt;&lt;br /&gt;&amp;lt;sonar.jacoco.reportPath&amp;gt;&lt;br /&gt;  ${coverage.reports.dir}/jacoco-unit.exec&lt;br /&gt;&amp;lt;/sonar.jacoco.reportPath&amp;gt;&lt;br /&gt;&amp;lt;sonar.jacoco.itReportPath&amp;gt;&lt;br /&gt;  ${coverage.reports.dir}/jacoco-it.exec&lt;br /&gt;&amp;lt;/sonar.jacoco.itReportPath&amp;gt;&lt;br /&gt;&amp;lt;sonar.jacoco.jar&amp;gt;&lt;br /&gt;  ${basedir}/lib/jacocoagent.jar&lt;br /&gt;&amp;lt;/sonar.jacoco.jar&amp;gt;&lt;br /&gt;&lt;/pre&gt;Then we start the unit tests by running the standard the surefire plugin with the Jacoco agent pointing to &lt;b&gt;${sonar.jacoco.&lt;i&gt;reportPath}&lt;/i&gt;&lt;/b&gt;.&amp;nbsp; This is used to store the unit test code coverage results.&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;plugin&amp;gt;&lt;br /&gt;    &amp;lt;groupId&amp;gt;org.apache.maven.plugins&amp;lt;/groupId&amp;gt;&lt;br /&gt;    &amp;lt;artifactId&amp;gt;maven-surefire-plugin&amp;lt;/artifactId&amp;gt;&lt;br /&gt;    &amp;lt;version&amp;gt;2.7.2&amp;lt;/version&amp;gt;&lt;br /&gt;    &amp;lt;configuration&amp;gt;&lt;br /&gt;    &lt;b&gt;&amp;lt;argLine&amp;gt;          &lt;br /&gt;       -javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.reportPath}       &lt;br /&gt;    &amp;lt;/argLine&amp;gt;&lt;/b&gt;&lt;br /&gt;    &amp;lt;test&amp;gt;**/*.java&amp;lt;/test&amp;gt;&lt;br /&gt;    &amp;lt;/configuration&amp;gt;&lt;br /&gt;&amp;lt;/plugin&amp;gt;&lt;br /&gt;&lt;/pre&gt;For the Integration tests we use the failsafe plugin and point the Jacoco agent to &lt;b&gt;${sonar.jacoco.&lt;i&gt;itReportPath}&lt;/i&gt;&lt;/b&gt;.&amp;nbsp; This is used to store the integration test code coverage results.&lt;i&gt;&lt;b&gt;&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;plugin&amp;gt;&lt;br /&gt;  &amp;lt;artifactId&amp;gt;maven-failsafe-plugin&amp;lt;/artifactId&amp;gt;&lt;br /&gt;  &amp;lt;version&amp;gt;2.8&amp;lt;/version&amp;gt;&lt;br /&gt;  &amp;lt;configuration&amp;gt;         &lt;br /&gt;    &amp;lt;argLine&amp;gt;&lt;br /&gt;      -javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.itReportPath}&lt;br /&gt;    &amp;lt;/argLine&amp;gt;&lt;br /&gt;   &amp;lt;testClassesDirectory&amp;gt;&lt;br /&gt;      ${integrationOutputDirectory}&lt;br /&gt;   &amp;lt;/testClassesDirectory&amp;gt;&lt;br /&gt;   &amp;lt;reportsDirectory&amp;gt;&lt;br /&gt;     ${integrationOutputDirectory}/failsafe-reports&lt;br /&gt;   &amp;lt;/reportsDirectory&amp;gt;&lt;br /&gt;   &amp;lt;test&amp;gt;**/*.java&amp;lt;/test&amp;gt;&lt;br /&gt;   &amp;lt;additionalClasspathElements&amp;gt;&lt;br /&gt;     &amp;lt;additionalClasspathElement&amp;gt;&lt;br /&gt;       ${integrationSourceDirectory}/resources&amp;lt;/additionalClasspathElement&amp;gt;&lt;br /&gt;     &amp;lt;/additionalClasspathElements&amp;gt;&lt;br /&gt;   &amp;lt;/configuration&amp;gt;&lt;br /&gt;   &amp;lt;executions&amp;gt;&lt;br /&gt;     &amp;lt;execution&amp;gt;&lt;br /&gt;       &amp;lt;goals&amp;gt;&lt;br /&gt;          &amp;lt;goal&amp;gt;integration-test&amp;lt;/goal&amp;gt;&lt;br /&gt;       &amp;lt;/goals&amp;gt;&lt;br /&gt;     &amp;lt;/execution&amp;gt;&lt;br /&gt;  &amp;lt;/executions&amp;gt;&lt;br /&gt;&amp;lt;/plugin&amp;gt;&lt;/pre&gt;&lt;br /&gt;When these are run they will produce the following 2 coverage files.&lt;br /&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-9QvY7D-nYYE/TpWttQRss9I/AAAAAAAAAFM/S5hLpLm2r8Q/s1600/reports.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;Running Sonar&lt;/span&gt;&lt;br /&gt;We use the standard sonar:sonar target however we first compile and run our tests.&lt;br /&gt;This will cause the unit and integration tests to run as described above.&lt;br /&gt;The following parameter tells Sonar to use jacoco as the default code coverage plugin&lt;br /&gt;&lt;pre class="brush:xml;"&gt;&amp;lt;sonar.core.codeCoveragePlugin&amp;gt;jacoco&amp;lt;/sonar.core.codeCoveragePlugin&amp;gt;&lt;br /&gt;&lt;/pre&gt;We then specify for Sonar to re-use the created coverage files.&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;sonar.dynamicAnalysis&amp;gt;reuseReports&amp;lt;/sonar.dynamicAnalysis&amp;gt;&lt;/pre&gt;We then run the whole process by running the following Maven command&lt;br /&gt;&lt;pre class="brush:xml"&gt;mvn –Psonar,it clean install sonar:sonar&lt;/pre&gt;This produces the results discussed above in section 1.3. Results Of Running the Examples&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;2.3 &amp;nbsp;Using a Profile&lt;/span&gt;&lt;br /&gt;The sonar profile tidies all of these pieces up into one reusable unit. The full profile is here.&lt;br /&gt;&lt;a href="https://designbycontract.googlecode.com/svn/trunk/sonar/pom.xml"&gt;https://designbycontract.googlecode.com/svn/trunk/sonar/pom.xml&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-size: large;"&gt;2.4 &amp;nbsp;Next Steps&lt;/span&gt;&lt;br /&gt;You can look at a multi-module example of the same technique here:&lt;br /&gt;&amp;nbsp;&lt;a href="http://johndobie.blogspot.com/2011/06/maven-multi-module-with-sonar-and.html"&gt;http://johndobie.blogspot.com/2011/06/maven-multi-module-with-sonar-and.html&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-2648347740699454566?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/2648347740699454566/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/2648347740699454566'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/2648347740699454566'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html' title='Separating Code Coverage With Maven, Sonar and Jacoco'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-W7s50lNbfJw/Tei-n4nfKqI/AAAAAAAAABE/5vz45RWpnr8/s72-c/sonar.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6293455831777973426.post-1517054451525202160</id><published>2011-06-03T10:01:00.240+01:00</published><updated>2011-09-16T12:42:47.457+01:00</updated><title type='text'>Separating Maven Unit &amp; Integration Tests</title><content type='html'>&lt;h1&gt;1. Introduction&lt;/h1&gt;In this example I will show how you can perform a standard Maven build whilst keeping your unit and integration tests in separate packages.&lt;br /&gt;&lt;br /&gt;Other techniques specify using either a naming convention or a separate module to do this. &lt;br /&gt;This example allows true package separation and is self contained in a single profile so it can easily be used across projects. Alternative profiles can also be created for different types of test.&lt;br /&gt;&lt;br /&gt;This solution lends itself well to generating code coverage metrics using Sonar and Jacoco.&lt;br /&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html" target="_blank"&gt;http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The first section shows what happens in the example and how to run it. &lt;br /&gt;The second section explains how it works.&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;1.1 Example Structure&lt;/h1&gt;Here we have the typical maven structure and our new folder &lt;folder&gt;\src\integrationtest\java&lt;/folder&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/-PX0IRd0v8OE/TdKNt0wsOQI/AAAAAAAAAAM/YGCE_pqZVq4/s1600/structure.jpg"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-PX0IRd0v8OE/TdKNt0wsOQI/AAAAAAAAAAM/YGCE_pqZVq4/s1600/structure.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;1.2 Running the Example&lt;/h1&gt;The full code is hosted at google code.&amp;nbsp; Use the following commands to check it out and run it.&lt;br /&gt;&lt;pre class="brush:bash"&gt;svn co https://designbycontract.googlecode.com/svn/trunk/failsafe&lt;br /&gt;cd failsafe&lt;br /&gt;mvn –Pit clean integration-test&lt;br /&gt;&lt;/pre&gt;Alternatively you can look at the Hudson build here : &lt;br /&gt;&lt;a href="https://designbycontract.ci.cloudbees.com/job/maven_with_split_tests/" target="_blank"&gt;https://designbycontract.ci.cloudbees.com/job/maven_with_split_tests/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;1.3 Results of running the example&lt;/h1&gt;&lt;ul&gt;&lt;li&gt;The tests in the standard maven test structure are run during the unit test phase as usual. &lt;br /&gt;&lt;/li&gt;&lt;li&gt;The tests in the &lt;folder&gt;\src\integrationtest\java &lt;/folder&gt;directory are run during the integration test phase. &lt;br /&gt;&lt;/li&gt;&lt;li&gt;The integration test classes are placed in the seperate &lt;folder&gt;\target\integrationtest-classes&lt;/folder&gt; directory&lt;/li&gt;&lt;/ul&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-s1mkC7ao6-0/Te5MPvYlcjI/AAAAAAAAABk/pra5iTrUTZ8/s1600/running-results.jpg" /&gt;&lt;br /&gt;&lt;h1&gt;1.4&amp;nbsp; Isn't there an easier way?&lt;/h1&gt;Maven doesn’t really have a tidy way of dealing with this.&amp;nbsp;Common suggestions are discussed here.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://olemortenamundsen.wordpress.com/2009/07/22/strategies-for-separating-unit-and-integration-tests-using-maven-eclipse-idea-cobertura/"&gt;http://olemortenamundsen.wordpress.com/2009/07/22/strategies-for-separating-unit-and-integration-tests-using-maven-eclipse-idea-cobertura/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;The main ones are&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Use \src\test package and use a separate directory or naming convention. &lt;br /&gt;&lt;/li&gt;&lt;li&gt;Put the integration tests in a separate module.&lt;/li&gt;&lt;/ul&gt;Maven does allow you to change the test source directories as demonstrated. The problem is that the Maven properties are immutable so you can't change them twice in the same build.  &lt;br /&gt;&lt;br /&gt;Firstly, we need to keep them at the defaults to enable our unit tests to run correctly.  &lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;testSourceDirectory&amp;gt;src\test\java&amp;lt;/testSourceDirectory&amp;gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;lt;testOutputDirectory&amp;gt;target\test-classes&amp;lt;/testOutputDirectory&amp;gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Secondly we need to change them as below for our integration tests to be run correctly.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;testSourceDirectory&amp;gt;src\integrationtest\java&amp;lt;/testSourceDirectory&amp;gt;&amp;nbsp; &lt;br /&gt;&amp;lt;testOutputDirectory&amp;gt;target\integrationtest-classes&amp;lt;/testOutputDirectory&amp;gt;  &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Unfortunately we cannot do this because the properties are immutable and cannot be both in the same build. However by using a number of different plugins and settings we can get the above scenario to work.&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;2. So what’s the Solution?&lt;/h1&gt;There are a number of discrete actions.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Firstly define variables for your integration test source and target directories&lt;/li&gt;&lt;li&gt;Use ant to create the output directory for the integration tests&lt;/li&gt;&lt;li&gt;Use the build helper plugin to add your test source to the compile path and copy any resources. &lt;/li&gt;&lt;li&gt;Compile the integration tests using -d to specify the path to the integration test source&lt;/li&gt;&lt;li&gt;Use the failsafe plugin to run the tests using parameters to specify the correct directories&lt;/li&gt;&lt;li&gt;Put it all in a profile so it can be used conveniently &lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;These are shown in the lifecycle diagram below.&lt;br /&gt;&lt;br /&gt;&lt;img border="0" height="576" src="http://4.bp.blogspot.com/-pSWqRnbIKlk/Te-ZFd_UM-I/AAAAAAAAAB0/iDpR5_GlSsU/s640/flowchart.jpg" width="640" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;2.1&amp;nbsp; Define 2 Variables pointing to the integration tests&lt;/h1&gt;We need to define where the integration test classes are and where to compile and run them from.&lt;a href="http://2.bp.blogspot.com/-kIx-Hk-Mjk8/Te-PkaGTVdI/AAAAAAAAABo/d9g9rDI9Sf0/s1600/variables.jpg" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-kIx-Hk-Mjk8/Te-PkaGTVdI/AAAAAAAAABo/d9g9rDI9Sf0/s1600/variables.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;properties&amp;gt;&lt;br /&gt;&amp;lt;integrationSourceDirectory&amp;gt;src\integrationtest&amp;lt;/integrationSourceDirectory&amp;gt;&lt;br /&gt;&amp;lt;integrationOutputDirectory&amp;gt;target\integrationtest-classes&amp;lt;/integrationOutputDirectory&amp;gt;&lt;br /&gt;&amp;lt;/properties&amp;gt;&lt;/pre&gt;&lt;h1&gt;2.2&amp;nbsp; Create The Output Directory&lt;/h1&gt;Use the Maven antrun plugin to create the output directory &lt;folder&gt;\target\integrationtest-classes&lt;/folder&gt;&lt;b style="font-style: italic;"&gt;&lt;folder&gt; &lt;/folder&gt;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;This is needed for the compiler which won't create it!&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;execution&amp;gt;&lt;br /&gt;&amp;lt;id&amp;gt;create-directory&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;phase&amp;gt;pre-integration-test&amp;lt;/phase&amp;gt;&lt;br /&gt;&amp;lt;goals&amp;gt;&lt;br /&gt;&amp;lt;goal&amp;gt;run&amp;lt;/goal&amp;gt;&lt;br /&gt;&amp;lt;/goals&amp;gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&amp;lt;tasks&amp;gt;&lt;br /&gt;&amp;lt;echo message="Creating Directory ${integrationOutputDirectory}"/&amp;gt;&lt;br /&gt;&amp;lt;mkdir dir="${integrationOutputDirectory}" /&amp;gt;&lt;br /&gt;&amp;lt;/tasks&amp;gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;br /&gt;&amp;lt;/execution&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h1&gt;2.3&amp;nbsp; Use Build Helper To Add The Test Source And Resources&lt;/h1&gt;First add the classes in &lt;folder&gt;src\integrationtest\java&lt;/folder&gt; to the compile path&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;execution&amp;gt;&lt;br /&gt;&amp;lt;id&amp;gt;add-test-sources&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;phase&amp;gt;pre-integration-test&amp;lt;/phase&amp;gt;&lt;br /&gt;&amp;lt;goals&amp;gt;&lt;br /&gt;&amp;lt;goal&amp;gt;add-test-source&amp;lt;/goal&amp;gt;&lt;br /&gt;&amp;lt;/goals&amp;gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&amp;lt;sources&amp;gt;&lt;br /&gt;&amp;lt;source&amp;gt;${integrationSourceDirectory}/java&amp;lt;/source&amp;gt;&lt;br /&gt;&amp;lt;/sources&amp;gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;br /&gt;&amp;lt;/execution&amp;gt;&lt;br /&gt;&lt;/pre&gt;Copy any resources from&lt;folder&gt; \src\integrationtest\resource&lt;/folder&gt; to &lt;folder&gt;\target\integration-classes&lt;/folder&gt;&lt;br /&gt;&lt;folder&gt;&lt;/folder&gt;This is not strictly necessary to get things working but allows easy packaging if you required.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;execution&amp;gt;&lt;br /&gt;&amp;lt;id&amp;gt;add-test-resources&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;phase&amp;gt;pre-integration-test&amp;lt;/phase&amp;gt;&lt;br /&gt;&amp;lt;goals&amp;gt;&lt;br /&gt;&amp;lt;goal&amp;gt;add-test-resource&amp;lt;/goal&amp;gt;&lt;br /&gt;&amp;lt;/goals&amp;gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;&amp;lt;resource&amp;gt;&lt;br /&gt;&amp;lt;directory&amp;gt;${integrationSourceDirectory}/java&amp;lt;/directory&amp;gt;&lt;br /&gt;&amp;lt;targetPath&amp;gt;${integrationOutputDirectory}&amp;lt;/targetPath&amp;gt;&lt;br /&gt;&amp;lt;/resource&amp;gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;br /&gt;&amp;lt;/execution&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;2.4. Compile The Classes Using -d to Control the location&lt;/h1&gt;By default your classes will be compiled to &lt;folder&gt;\target\classes&lt;/folder&gt;&lt;br /&gt;This forces your classes to be output to &lt;folder&gt;target\integrationtest-classes&lt;/folder&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;execution&amp;gt;&lt;br /&gt;&amp;lt;phase&amp;gt;pre-integration-test&amp;lt;/phase&amp;gt;&lt;br /&gt;&amp;lt;goals&amp;gt;&lt;br /&gt;&amp;lt;goal&amp;gt;testCompile&amp;lt;/goal&amp;gt;&lt;br /&gt;&amp;lt;/goals&amp;gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&amp;lt;compilerArguments&amp;gt;&lt;br /&gt;&amp;lt;d&amp;gt;${integrationOutputDirectory}&amp;lt;/d&amp;gt;&lt;br /&gt;&amp;lt;/compilerArguments&amp;gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;br /&gt;&amp;lt;/execution&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/-xlcTAJj72XM/Te-R9LPUtmI/AAAAAAAAABs/uaQp_izBhDc/s1600/integration-classes.jpg" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-xlcTAJj72XM/Te-R9LPUtmI/AAAAAAAAABs/uaQp_izBhDc/s1600/integration-classes.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;h1&gt;2.5 Use Failsafe to run the tests and customise the locations&lt;/h1&gt;This runs all tests in&lt;folder&gt; \target\integrationtest-classes&lt;/folder&gt;&lt;br /&gt;The failsafe reports are put in &lt;folder&gt;\target\integrationtest-classes\failsafe-reports.&lt;/folder&gt;&lt;br /&gt;&lt;pre class="brush:xml"&gt;&amp;lt;testClassesDirectory&amp;gt;${integrationOutputDirectory}&amp;lt;/testClassesDirectory&amp;gt;&lt;br /&gt;&amp;lt;reportsDirectory&amp;gt;${integrationOutputDirectory}/failsafe-reports&amp;lt;/reportsDirectory&amp;gt;&lt;br /&gt;&amp;lt;test&amp;gt;**/*.java&amp;lt;/test&amp;gt;&lt;br /&gt;&amp;lt;additionalClasspathElements&amp;gt;&lt;br /&gt;&amp;lt;additionalClasspath&amp;gt;${integrationSourceDirectory}/resources&amp;lt;/additionalClasspath&amp;gt;&lt;br /&gt;&amp;lt;/additionalClasspathElements&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/-n0zMfcFofLw/Te-TQXEC35I/AAAAAAAAABw/wQnyGZXeI0s/s1600/failsafe.jpg" imageanchor="1" style="float: none; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" imageanchor="1" src="http://1.bp.blogspot.com/-n0zMfcFofLw/Te-TQXEC35I/AAAAAAAAABw/wQnyGZXeI0s/s1600/failsafe.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;h1&gt;2.6 The 'IT' Profile&lt;/h1&gt;The implementation of this is placed in a profile ‘it'&lt;br /&gt;&lt;a href="https://designbycontract.googlecode.com/svn/trunk/failsafe/pom.xml"&gt;https://designbycontract.googlecode.com/svn/trunk/failsafe/pom.xml&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;2.7 Wrapping Up.&lt;/h1&gt;&lt;br /&gt;This is a simple solution that can be rolled out to teams even if they are not strong in Maven.&lt;br /&gt;By providing the profile, they only need to define their own paths.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;2.7 Whats Next.&lt;/h1&gt;&lt;br /&gt;Check out how to add code coverage metrics usign Sonar.&lt;br /&gt;&lt;a href="http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html"&gt;http://johndobie.blogspot.com/2011/06/seperating-test-code-coverage-with.html&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6293455831777973426-1517054451525202160?l=johndobie.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://johndobie.blogspot.com/feeds/1517054451525202160/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/1517054451525202160'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6293455831777973426/posts/default/1517054451525202160'/><link rel='alternate' type='text/html' href='http://johndobie.blogspot.com/2011/06/seperating-maven-unit-integration-tests.html' title='Separating Maven Unit &amp;amp; Integration Tests'/><author><name>jdobie</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-PX0IRd0v8OE/TdKNt0wsOQI/AAAAAAAAAAM/YGCE_pqZVq4/s72-c/structure.jpg' height='72' width='72'/><thr:total>3</thr:total></entry></feed>
