1<html> 2 <head> 3 <title>TestNG - Migrating from JUnit</title> 4 5 <link rel="stylesheet" href="testng.css" type="text/css" /> 6 <link type="text/css" rel="stylesheet" href="http://beust.com/beust.css" /> 7 <script type="text/javascript" src="http://beust.com/prettify.js"></script> 8 <script type="text/javascript" src="banner.js"></script> 9 10 <script type="text/javascript" src="http://beust.com/scripts/shCore.js"></script> 11 <script type="text/javascript" src="http://beust.com/scripts/shBrushJava.js"></script> 12 <script type="text/javascript" src="http://beust.com/scripts/shBrushXml.js"></script> 13 <script type="text/javascript" src="http://beust.com/scripts/shBrushBash.js"></script> 14 <script type="text/javascript" src="http://beust.com/scripts/shBrushPlain.js"></script> 15 <link type="text/css" rel="stylesheet" href="http://beust.com/styles/shCore.css"/> 16 <link type="text/css" rel="stylesheet" href="http://beust.com/styles/shThemeCedric.css"/> 17 <script type="text/javascript"> 18 SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf'; 19 SyntaxHighlighter.defaults['gutter'] = false; 20 SyntaxHighlighter.all(); 21 </script> 22 23 </head> 24<body onLoad="prettyPrint()"> 25 26<script type="text/javascript"> 27 displayMenu("migrating.html") 28</script> 29 30 31<h2 align="center">Migrating from JUnit</h2> 32 33<h3>Using Eclipse</h3> 34 35The 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 37<h3>Asserts</h3> 38Note 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 40<pre class="brush: java"> 41import static org.testng.AssertJUnit.*; 42</pre> 43 44<h3>Running JUnit Tests</h3> 45 46<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 48<p>All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes, 49change your test runner from JUnit to TestNG in Ant and then run TestNG in <tt>"mixed"</tt> mode. 50This way you can have all your tests in the same project, even in the same package, and start using TestNG. 51This approach also allows you to convert your existing JUnit tests to TestNG incrementally.</p> 52 53<h4>Example - replacing JUnit Ant task with TestNG one</h4> 54 55JUnit version: 56<pre class="brush: xml"> 57<junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true"> 58 <batchtest todir="${build.test.results.dir}"> 59 <fileset dir="${test.src.dir}"> 60 <include name="**/*Test.*"/> 61 </batchtest> 62 <classpath> 63 <path path="${run.test.classpath}"/> 64 </classpath> 65 <syspropertyset> 66 <propertyref prefix="test-sys-prop."/> 67 <mapper from="test-sys-prop.*" to="*" type="glob"/> 68 </syspropertyset> 69 <formatter type="xml"/> 70 <jvmarg value="-ea"/> 71 <jvmarg line="${run.jvmargs}"/> 72</junit> 73</pre> 74 75TestNG version: 76<pre class="brush: xml"> 77<taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/> 78 79<fileset id="mixed.tests" dir="${test.src.dir}"> 80 <include name="**/*Test.*"/> 81</fileset> 82 83<testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}"> 84 <classpath> 85 <pathelement path="${build.test.classes.dir}"/> 86 <pathelement path="${run.test.classpath}"/> 87 <pathelement path="${junit.lib}"/> 88 </classpath> 89 <propertyset> 90 <propertyref prefix="test-sys-prop."/> 91 <mapper from="test-sys-prop.*" to="*" type="glob"/> 92 </propertyset> 93 <jvmarg line="${run.jvmargs}"/> 94</testng> 95</pre> 96 97 98<h3>Related reading</h3> 99 100<ul> 101 <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 <li><a href="http://wiki.netbeans.org/TestNG_MixedMode">Mixed mode in TestNG</a>.</li> 103</ul> 104 105<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> 106</script> 107<script type="text/javascript"> 108_uacct = "UA-238215-2"; 109urchinTracker(); 110</script> 111 112 113</body> 114