-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmigrating_from_junit.html
125 lines (125 loc) · 5.23 KB
/
migrating_from_junit.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.18">
<title>Migrating from JUnit</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
</head>
<body class="article toc2 toc-left">
<div id="header">
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_migrating_from_junit">Migrating from JUnit</a>
<ul class="sectlevel2">
<li><a href="#_using_eclipse">Using Eclipse</a></li>
<li><a href="#_asserts">Asserts</a></li>
<li><a href="#_running_junit_tests">Running JUnit Tests</a></li>
<li><a href="#_related_reading">Related reading</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="content">
<div class="sect1">
<h2 id="_migrating_from_junit">Migrating from JUnit</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_using_eclipse">Using Eclipse</h3>
<div class="paragraph">
<p>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="https://testng.org/doc/eclipse.html#eclipse-quickfix">Eclipse section</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="_asserts">Asserts</h3>
<div class="paragraph">
<p>Note that the class <code>org.testng.Assert</code> 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 use a static import of that class:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import static org.testng.AssertJUnit.*;</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_running_junit_tests">Running JUnit Tests</h3>
<div class="paragraph">
<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>
</div>
<div class="paragraph">
<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 "mixed" 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>
</div>
<div class="paragraph">
<p><strong>Example - replacing JUnit Ant task with TestNG one</strong></p>
</div>
<div class="paragraph">
<p>JUnit version:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml"><junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true">
<batchtest todir="${build.test.results.dir}">
<fileset dir="${test.src.dir}">
<include name="**/*Test.*"/>
</batchtest>
<classpath>
<path path="${run.test.classpath}"/>
</classpath>
<syspropertyset>
<propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/>
</syspropertyset>
<formatter type="xml"/>
<jvmarg value="-ea"/>
<jvmarg line="${run.jvmargs}"/>
</junit></code></pre>
</div>
</div>
<div class="paragraph">
<p>TestNG version:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml"><taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/>
<fileset id="mixed.tests" dir="${test.src.dir}">
<include name="**/*Test.*"/>
</fileset>
<testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}">
<classpath>
<pathelement path="${build.test.classes.dir}"/>
<pathelement path="${run.test.classpath}"/>
<pathelement path="${junit.lib}"/>
</classpath>
<propertyset>
<propertyref prefix="test-sys-prop."/>
<mapper from="test-sys-prop.*" to="*" type="glob"/>
</propertyset>
<jvmarg line="${run.jvmargs}"/>
</testng></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_related_reading">Related reading</h3>
<div class="paragraph">
<p><a href="https://web.archive.org/web/20150214034607/http://developers.opengamma.com/blog/2011/04/04/converting-opengamma-junit-testng">Here</a> is the detailed report of a company that successfully converted a large codebase of JUnit 4 tests over to TestNG.</p>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2025-02-27 18:51:48 UTC
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>
</body>
</html>