blob: 1767c8a9d148e0329b87270411dd2abd444e756a [file] [log] [blame]
<html>
<head>
<title>TestNG - Migrating from JUnit</title>
<link rel="stylesheet" href="testng.css" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://beust.com/beust.css" />
<script type="text/javascript" src="http://beust.com/prettify.js"></script>
<script type="text/javascript" src="banner.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shCore.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushJava.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushXml.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushBash.js"></script>
<script type="text/javascript" src="http://beust.com/scripts/shBrushPlain.js"></script>
<link type="text/css" rel="stylesheet" href="http://beust.com/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="http://beust.com/styles/shThemeCedric.css"/>
<script type="text/javascript">
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['gutter'] = false;
SyntaxHighlighter.all();
</script>
</head>
<body onLoad="prettyPrint()">
<script type="text/javascript">
displayMenu("migrating.html")
</script>
<h2 align="center">Migrating from JUnit</h2>
<h3>Using Eclipse</h3>
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>.
<h3>Asserts</h3>
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:
<pre class="brush: java">
import static org.testng.AssertJUnit.*;
</pre>
<h3>Running JUnit Tests</h3>
<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>
<p>All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes,
change your test runner from JUnit to TestNG in Ant and then run TestNG in <tt>"mixed"</tt> mode.
This way you can have all your tests in the same project, even in the same package, and start using TestNG.
This approach also allows you to convert your existing JUnit tests to TestNG incrementally.</p>
<h4>Example - replacing JUnit Ant task with TestNG one</h4>
JUnit version:
<pre class="brush: xml">
&lt;junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true"&gt;
&lt;batchtest todir="${build.test.results.dir}"&gt;
&lt;fileset dir="${test.src.dir}"&gt;
&lt;include name="**/*Test.*"/&gt;
&lt;/batchtest&gt;
&lt;classpath&gt;
&lt;path path="${run.test.classpath}"/&gt;
&lt;/classpath&gt;
&lt;syspropertyset&gt;
&lt;propertyref prefix="test-sys-prop."/&gt;
&lt;mapper from="test-sys-prop.*" to="*" type="glob"/&gt;
&lt;/syspropertyset&gt;
&lt;formatter type="xml"/&gt;
&lt;jvmarg value="-ea"/&gt;
&lt;jvmarg line="${run.jvmargs}"/&gt;
&lt;/junit&gt;
</pre>
TestNG version:
<pre class="brush: xml">
&lt;taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/&gt;
&lt;fileset id="mixed.tests" dir="${test.src.dir}"&gt;
&lt;include name="**/*Test.*"/&gt;
&lt;/fileset&gt;
&lt;testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}"&gt;
&lt;classpath&gt;
&lt;pathelement path="${build.test.classes.dir}"/&gt;
&lt;pathelement path="${run.test.classpath}"/&gt;
&lt;pathelement path="${junit.lib}"/&gt;
&lt;/classpath&gt;
&lt;propertyset&gt;
&lt;propertyref prefix="test-sys-prop."/&gt;
&lt;mapper from="test-sys-prop.*" to="*" type="glob"/&gt;
&lt;/propertyset&gt;
&lt;jvmarg line="${run.jvmargs}"/&gt;
&lt;/testng&gt;
</pre>
<h3>Related reading</h3>
<ul>
<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>
<li><a href="http://wiki.netbeans.org/TestNG_MixedMode">Mixed mode in TestNG</a>.</li>
</ul>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-238215-2";
urchinTracker();
</script>
</body>