31
31
<h2 align="center">Migrating from JUnit</h2>
33
<h3>Using JUnitConverter</h3>
34
<p>You can easily convert your JUnit tests with <tt>org.testng.JUnitConverter,</tt>
35
which you can invoke as follows:</p><ul>
36
<li><tt>-annotation | -javadoc</tt> (required)<br>
37
If <tt>-annotation</tt>, <tt>
38
JUnitConverter</tt> will insert JDK5 annotations, and if you specify <tt>-javadoc</tt>,
39
the tool will insert JavaDoc annotations.<br>
40
</li><li><tt>-srcdir | -source <fileName></tt> (required)<br>
41
<tt>-srcdir</tt>, the directory
42
will be recursively traversed and any file that ends in .java will be
43
processed. If you specify <tt>-source</tt>, only that specific Java
44
file will be processed.<br>
45
</li><li><tt>-d | -overwrite</tt> (mandatory)<br>
46
By default, <tt>JUnitConverter </tt>will modify the sources it finds.
47
If you specify a directory with this option, this is where the modified
48
files will be created. If you specify <tt>-overwrite</tt>, the source
49
files will be directly modified.<br>
50
</li><li><tt>-quiet</tt><br>
51
<tt>JUnitConverter</tt> will not print anything on the console.</li></ul>
53
<p>Here is a sample use that will convert all the JUnit tests in the <tt>src/</tt>
54
directory to TestNG:</p>
56
<pre class="brush: text">
57
java org.testng.JUnitConverter -overwrite -annotation -srcdir src
61
<li><i><tt>JUnitConverter</tt> uses classes from <tt>tools.jar</tt>, which is located in
62
<tt>$JAVA_HOME/lib/tools.jar</tt>,
63
so make sure you have this jar file in your classpath.<br>
65
<li><i><tt>JUnitConverter</tt>
66
only runs with the JDK5, so if you receive an error (such as "unknown -quiet
67
parameter"), make sure your classpath only contains JDK5 jar files.</i></li>
69
</blockquote><p><tt>JUnitConverter</tt> also has an ant task, which you can invoke as follows: </p>
71
<pre class="brush: xml">
72
<project name="test" default="init">
73
<target name="init">
74
<taskdef resource="testngtasks" />
77
<target name="junitconvert" depends="init">
78
<junit-converter sourcedir="C:\dev\projects\test\java" outputdir="C:\dev\projects\temp" annotations="false" />
33
<h3>Using Eclipse</h3>
35
The 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>.
84
38
Note 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:
86
40
<pre class="brush: java">
87
41
import static org.testng.AssertJUnit.*;
44
<h3>Running JUnit Tests</h3>
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>
48
<p>All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes,
49
change your test runner from JUnit to TestNG in Ant and then run TestNG in <tt>"mixed"</tt> mode.
50
This way you can have all your tests in the same project, even in the same package, and start using TestNG.
51
This approach also allows you to convert your existing JUnit tests to TestNG incrementally.</p>
53
<h4>Example - replacing JUnit Ant task with TestNG one</h4>
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.*"/>
63
<path path="${run.test.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}"/>
76
<pre class="brush: xml">
77
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/>
79
<fileset id="mixed.tests" dir="${test.src.dir}">
80
<include name="**/*Test.*"/>
83
<testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}">
85
<pathelement path="${build.test.classes.dir}"/>
86
<pathelement path="${run.test.classpath}"/>
87
<pathelement path="${junit.lib}"/>
90
<propertyref prefix="test-sys-prop."/>
91
<mapper from="test-sys-prop.*" to="*" type="glob"/>
93
<jvmarg line="${run.jvmargs}"/>
98
<h3>Related reading</h3>
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>
91
105
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">