| <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 JUnitConverter</h3> |
| <p>You can easily convert your JUnit tests with <tt>org.testng.JUnitConverter,</tt> |
| which you can invoke as follows:</p><ul> |
| <li><tt>-annotation | -javadoc</tt> (required)<br> |
| If <tt>-annotation</tt>, <tt> |
| JUnitConverter</tt> will insert JDK5 annotations, and if you specify <tt>-javadoc</tt>, |
| the tool will insert JavaDoc annotations.<br> |
| </li><li><tt>-srcdir | -source <fileName></tt> (required)<br> |
| <tt>-srcdir</tt>, the directory |
| will be recursively traversed and any file that ends in .java will be |
| processed. If you specify <tt>-source</tt>, only that specific Java |
| file will be processed.<br> |
| </li><li><tt>-d | -overwrite</tt> (mandatory)<br> |
| By default, <tt>JUnitConverter </tt>will modify the sources it finds. |
| If you specify a directory with this option, this is where the modified |
| files will be created. If you specify <tt>-overwrite</tt>, the source |
| files will be directly modified.<br> |
| </li><li><tt>-quiet</tt><br> |
| <tt>JUnitConverter</tt> will not print anything on the console.</li></ul> |
| |
| <p>Here is a sample use that will convert all the JUnit tests in the <tt>src/</tt> |
| directory to TestNG:</p> |
| |
| <pre class="brush: text"> |
| java org.testng.JUnitConverter -overwrite -annotation -srcdir src |
| </pre> |
| Notes:<blockquote> |
| <ul> |
| <li><i><tt>JUnitConverter</tt> uses classes from <tt>tools.jar</tt>, which is located in |
| <tt>$JAVA_HOME/lib/tools.jar</tt>, |
| so make sure you have this jar file in your classpath.<br> |
| </i></li> |
| <li><i><tt>JUnitConverter</tt> |
| only runs with the JDK5, so if you receive an error (such as "unknown -quiet |
| parameter"), make sure your classpath only contains JDK5 jar files.</i></li> |
| </ul> |
| </blockquote><p><tt>JUnitConverter</tt> also has an ant task, which you can invoke as follows: </p> |
| |
| <pre class="brush: xml"> |
| <project name="test" default="init"> |
| <target name="init"> |
| <taskdef resource="testngtasks" /> |
| </target> |
| |
| <target name="junitconvert" depends="init"> |
| <junit-converter sourcedir="C:\dev\projects\test\java" outputdir="C:\dev\projects\temp" annotations="false" /> |
| </target> |
| </project> |
| </pre> |
| |
| <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> |
| |
| |
| <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> |
| </script> |
| <script type="text/javascript"> |
| _uacct = "UA-238215-2"; |
| urchinTracker(); |
| </script> |
| |
| |
| </body> |
| |