1*83a54b2fSSadaf Ebrahimi<html> 2*83a54b2fSSadaf Ebrahimi <head> 3*83a54b2fSSadaf Ebrahimi <title>TestNG - Migrating from JUnit</title> 4*83a54b2fSSadaf Ebrahimi 5*83a54b2fSSadaf Ebrahimi <link rel="stylesheet" href="testng.css" type="text/css" /> 6*83a54b2fSSadaf Ebrahimi <link type="text/css" rel="stylesheet" href="http://beust.com/beust.css" /> 7*83a54b2fSSadaf Ebrahimi <script type="text/javascript" src="http://beust.com/prettify.js"></script> 8*83a54b2fSSadaf Ebrahimi <script type="text/javascript" src="banner.js"></script> 9*83a54b2fSSadaf Ebrahimi 10*83a54b2fSSadaf Ebrahimi <script type="text/javascript" src="http://beust.com/scripts/shCore.js"></script> 11*83a54b2fSSadaf Ebrahimi <script type="text/javascript" src="http://beust.com/scripts/shBrushJava.js"></script> 12*83a54b2fSSadaf Ebrahimi <script type="text/javascript" src="http://beust.com/scripts/shBrushXml.js"></script> 13*83a54b2fSSadaf Ebrahimi <script type="text/javascript" src="http://beust.com/scripts/shBrushBash.js"></script> 14*83a54b2fSSadaf Ebrahimi <script type="text/javascript" src="http://beust.com/scripts/shBrushPlain.js"></script> 15*83a54b2fSSadaf Ebrahimi <link type="text/css" rel="stylesheet" href="http://beust.com/styles/shCore.css"/> 16*83a54b2fSSadaf Ebrahimi <link type="text/css" rel="stylesheet" href="http://beust.com/styles/shThemeCedric.css"/> 17*83a54b2fSSadaf Ebrahimi <script type="text/javascript"> 18*83a54b2fSSadaf Ebrahimi SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf'; 19*83a54b2fSSadaf Ebrahimi SyntaxHighlighter.defaults['gutter'] = false; 20*83a54b2fSSadaf Ebrahimi SyntaxHighlighter.all(); 21*83a54b2fSSadaf Ebrahimi </script> 22*83a54b2fSSadaf Ebrahimi 23*83a54b2fSSadaf Ebrahimi </head> 24*83a54b2fSSadaf Ebrahimi<body onLoad="prettyPrint()"> 25*83a54b2fSSadaf Ebrahimi 26*83a54b2fSSadaf Ebrahimi<script type="text/javascript"> 27*83a54b2fSSadaf Ebrahimi displayMenu("migrating.html") 28*83a54b2fSSadaf Ebrahimi</script> 29*83a54b2fSSadaf Ebrahimi 30*83a54b2fSSadaf Ebrahimi 31*83a54b2fSSadaf Ebrahimi<h2 align="center">Migrating from JUnit</h2> 32*83a54b2fSSadaf Ebrahimi 33*83a54b2fSSadaf Ebrahimi<h3>Using Eclipse</h3> 34*83a54b2fSSadaf Ebrahimi 35*83a54b2fSSadaf EbrahimiThe easiest way to convert your JUnit tests to TestNG is to use the Eclipse TestNG plug-in refactoring support. You will find a full description of its features in the <a href="eclipse.html#eclipse-quickfix">Eclipse section</a>. 36*83a54b2fSSadaf Ebrahimi 37*83a54b2fSSadaf Ebrahimi<h3>Asserts</h3> 38*83a54b2fSSadaf EbrahimiNote that the class <tt>org.testng.Assert</tt> uses a different argument ordering than the ones used by JUnit. If you are porting code that uses JUnit's asserts, you might want to us a static import of that class: 39*83a54b2fSSadaf Ebrahimi 40*83a54b2fSSadaf Ebrahimi<pre class="brush: java"> 41*83a54b2fSSadaf Ebrahimiimport static org.testng.AssertJUnit.*; 42*83a54b2fSSadaf Ebrahimi</pre> 43*83a54b2fSSadaf Ebrahimi 44*83a54b2fSSadaf Ebrahimi<h3>Running JUnit Tests</h3> 45*83a54b2fSSadaf Ebrahimi 46*83a54b2fSSadaf Ebrahimi<p>TestNG can automatically recognize and run JUnit tests, so you can use TestNG as a runner for all your existing tests and write new tests using TestNG.</p> 47*83a54b2fSSadaf Ebrahimi 48*83a54b2fSSadaf Ebrahimi<p>All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes, 49*83a54b2fSSadaf Ebrahimichange your test runner from JUnit to TestNG in Ant and then run TestNG in <tt>"mixed"</tt> mode. 50*83a54b2fSSadaf EbrahimiThis way you can have all your tests in the same project, even in the same package, and start using TestNG. 51*83a54b2fSSadaf EbrahimiThis approach also allows you to convert your existing JUnit tests to TestNG incrementally.</p> 52*83a54b2fSSadaf Ebrahimi 53*83a54b2fSSadaf Ebrahimi<h4>Example - replacing JUnit Ant task with TestNG one</h4> 54*83a54b2fSSadaf Ebrahimi 55*83a54b2fSSadaf EbrahimiJUnit version: 56*83a54b2fSSadaf Ebrahimi<pre class="brush: xml"> 57*83a54b2fSSadaf Ebrahimi<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true"> 58*83a54b2fSSadaf Ebrahimi <batchtest todir="${build.test.results.dir}"> 59*83a54b2fSSadaf Ebrahimi <fileset dir="${test.src.dir}"> 60*83a54b2fSSadaf Ebrahimi <include name="**/*Test.*"/> 61*83a54b2fSSadaf Ebrahimi </batchtest> 62*83a54b2fSSadaf Ebrahimi <classpath> 63*83a54b2fSSadaf Ebrahimi <path path="${run.test.classpath}"/> 64*83a54b2fSSadaf Ebrahimi </classpath> 65*83a54b2fSSadaf Ebrahimi <syspropertyset> 66*83a54b2fSSadaf Ebrahimi <propertyref prefix="test-sys-prop."/> 67*83a54b2fSSadaf Ebrahimi <mapper from="test-sys-prop.*" to="*" type="glob"/> 68*83a54b2fSSadaf Ebrahimi </syspropertyset> 69*83a54b2fSSadaf Ebrahimi <formatter type="xml"/> 70*83a54b2fSSadaf Ebrahimi <jvmarg value="-ea"/> 71*83a54b2fSSadaf Ebrahimi <jvmarg line="${run.jvmargs}"/> 72*83a54b2fSSadaf Ebrahimi</junit> 73*83a54b2fSSadaf Ebrahimi</pre> 74*83a54b2fSSadaf Ebrahimi 75*83a54b2fSSadaf EbrahimiTestNG version: 76*83a54b2fSSadaf Ebrahimi<pre class="brush: xml"> 77*83a54b2fSSadaf Ebrahimi<taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/> 78*83a54b2fSSadaf Ebrahimi 79*83a54b2fSSadaf Ebrahimi<fileset id="mixed.tests" dir="${test.src.dir}"> 80*83a54b2fSSadaf Ebrahimi <include name="**/*Test.*"/> 81*83a54b2fSSadaf Ebrahimi</fileset> 82*83a54b2fSSadaf Ebrahimi 83*83a54b2fSSadaf Ebrahimi<testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}"> 84*83a54b2fSSadaf Ebrahimi <classpath> 85*83a54b2fSSadaf Ebrahimi <pathelement path="${build.test.classes.dir}"/> 86*83a54b2fSSadaf Ebrahimi <pathelement path="${run.test.classpath}"/> 87*83a54b2fSSadaf Ebrahimi <pathelement path="${junit.lib}"/> 88*83a54b2fSSadaf Ebrahimi </classpath> 89*83a54b2fSSadaf Ebrahimi <propertyset> 90*83a54b2fSSadaf Ebrahimi <propertyref prefix="test-sys-prop."/> 91*83a54b2fSSadaf Ebrahimi <mapper from="test-sys-prop.*" to="*" type="glob"/> 92*83a54b2fSSadaf Ebrahimi </propertyset> 93*83a54b2fSSadaf Ebrahimi <jvmarg line="${run.jvmargs}"/> 94*83a54b2fSSadaf Ebrahimi</testng> 95*83a54b2fSSadaf Ebrahimi</pre> 96*83a54b2fSSadaf Ebrahimi 97*83a54b2fSSadaf Ebrahimi 98*83a54b2fSSadaf Ebrahimi<h3>Related reading</h3> 99*83a54b2fSSadaf Ebrahimi 100*83a54b2fSSadaf Ebrahimi<ul> 101*83a54b2fSSadaf Ebrahimi <li><a href="http://www.opengamma.com/blog/2011/04/04/converting-opengamma-junit-testng">Here is the detailed report of a company that successfully converted a large codebase of JUnit 4 tests over to TestNG</a>.</li> 102*83a54b2fSSadaf Ebrahimi <li><a href="http://wiki.netbeans.org/TestNG_MixedMode">Mixed mode in TestNG</a>.</li> 103*83a54b2fSSadaf Ebrahimi</ul> 104*83a54b2fSSadaf Ebrahimi 105*83a54b2fSSadaf Ebrahimi<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> 106*83a54b2fSSadaf Ebrahimi</script> 107*83a54b2fSSadaf Ebrahimi<script type="text/javascript"> 108*83a54b2fSSadaf Ebrahimi_uacct = "UA-238215-2"; 109*83a54b2fSSadaf EbrahimiurchinTracker(); 110*83a54b2fSSadaf Ebrahimi</script> 111*83a54b2fSSadaf Ebrahimi 112*83a54b2fSSadaf Ebrahimi 113*83a54b2fSSadaf Ebrahimi</body> 114*83a54b2fSSadaf Ebrahimi