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&lt;junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true"&gt;
58    &lt;batchtest todir="${build.test.results.dir}"&gt;
59        &lt;fileset dir="${test.src.dir}"&gt;
60            &lt;include name="**/*Test.*"/&gt;
61    &lt;/batchtest&gt;
62    &lt;classpath&gt;
63        &lt;path path="${run.test.classpath}"/&gt;
64    &lt;/classpath&gt;
65    &lt;syspropertyset&gt;
66        &lt;propertyref prefix="test-sys-prop."/&gt;
67        &lt;mapper from="test-sys-prop.*" to="*" type="glob"/&gt;
68    &lt;/syspropertyset&gt;
69    &lt;formatter type="xml"/&gt;
70    &lt;jvmarg value="-ea"/&gt;
71    &lt;jvmarg line="${run.jvmargs}"/&gt;
72&lt;/junit&gt;
73</pre>
74
75TestNG version:
76<pre class="brush: xml">
77&lt;taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/&gt;
78
79&lt;fileset id="mixed.tests" dir="${test.src.dir}"&gt;
80    &lt;include name="**/*Test.*"/&gt;
81&lt;/fileset&gt;
82
83&lt;testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}"&gt;
84    &lt;classpath&gt;
85        &lt;pathelement path="${build.test.classes.dir}"/&gt;
86        &lt;pathelement path="${run.test.classpath}"/&gt;
87        &lt;pathelement path="${junit.lib}"/&gt;
88    &lt;/classpath&gt;
89    &lt;propertyset&gt;
90        &lt;propertyref prefix="test-sys-prop."/&gt;
91        &lt;mapper from="test-sys-prop.*" to="*" type="glob"/&gt;
92    &lt;/propertyset&gt;
93    &lt;jvmarg line="${run.jvmargs}"/&gt;
94&lt;/testng&gt;
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