-
Notifications
You must be signed in to change notification settings - Fork 30
/
assertj-core-converting-junit-assertions-to-assertj.html
222 lines (175 loc) · 14.4 KB
/
assertj-core-converting-junit-assertions-to-assertj.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="AssertJ site">
<meta name="author" content="Joel Costigliola">
<title>AssertJ / Fluent assertions for java</title>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Inconsolata|Source+Code+Pro|Open+Sans|Ubuntu|Varela+Round|Karla">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet">
<script src="highlight/highlight.pack.js"></script>
<link rel="stylesheet" href="highlight/styles/railscasts.css">
<script>hljs.initHighlightingOnLoad();</script>
<link href="css/assertj.min.css" rel="stylesheet">
<link rel="shortcut icon" href="favicon.png" />
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- You'll want to use a responsive image option so this logo looks good on devices - I recommend using something like retina.js (do a quick Google search for it and you'll find it) -->
<a class="navbar-brand" href="index.html">AssertJ</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="assertj-core-quick-start.html">Quick start</a></li>
<li><a href="assertj-news.html">News</a></li>
<li><a href="assertj-core.html">Core</a></li>
<li><a href="assertj-assertions-generator.html">Assertions generator</a></li>
<li><a href="assertj-guava.html">Guava</a></li>
<li><a href="assertj-joda-time.html">Joda-Time</a></li>
<li><a href="assertj-db.html">DB</a></li>
<li><a href="assertj-neo4j.html">Neo4j</a></li>
<li><a href="assertj-swing.html">Swing</a></li>
<li><a href="assertj-help.html">Help</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-2 assertj-sidebar-menu">
<div class="bs-sidebar hidden-print affix-top" role="complementary">
<ul class="bs-sidenav nav ">
<li class="sidenav-header">Main</li>
<li><a href="assertj-core.html">Overview</a></li>
<li><a href="assertj-core-quick-start.html">Quick start</a></li>
<li><a href="assertj-core-news.html">News & releases</a></li>
<li><a href="assertj-core-features-highlight.html">Features highlight</a></li>
<li><a href="assertj-core-conditions.html">Using conditions</a></li>
<li><a href="assertj-core-custom-assertions.html">Custom assertions</a></li>
<li><a href="http://joel-costigliola.github.io/assertj/core/api/index.html" target="_blank">Javadoc (2.x)</a></li>
<li><a href="http://joel-costigliola.github.io/assertj/core-8/api/index.html" target="_blank">Javadoc (3.x)</a></li>
<li><a href="assertj-core.html#help">Help & F.A.Q</a></li>
<li><a href="assertj-core.html#code">Code & issues <i class="fa fa-github"></i></a></li>
<li><a href="assertj-core.html#team">Team</a></li>
<li><a href="assertj-core.html#contributing">Contributing</a></li>
<li class="sidenav-header">Migrating</li>
<li><a href="assertj-core-converting-junit-assertions-to-assertj.html">JUnit 4 Assertions</a></li>
<li><a href="assertj-core-converting-junit5-assertions-to-assertj.html">JUnit 5 Assertions</a></li>
<li><a href="assertj-core-converting-testng-assertions-to-assertj.html">TestNG Assertions</a></li>
<li><a href="assertj-core-migrating-from-fest.html">Fest Assert</a></li>
</ul>
</div>
</div>
<div class="col-lg-10 col-md-10 col-sm-10 text-left">
<h1 class="page-header">Converting your JUnit 3 & 4 assertions to AssertJ</h1>
<p>This page will help you converting your existing JUnit assertions to AssertJ ones. Note that both types of assertions can coexist, you don't have to migrate all at once.</p>
<p>The idea is to convert code like :<pre><code class="java">assertEquals(expected, actual);</code></pre> to :</p><p><pre><code class="java">assertThat(actual).isEqualTo(expected);</code></pre> </p>
<p>If you use JUnit 5 then check the <a href="assertj-core-converting-junit5-assertions-to-assertj.html">JUnit 5 Assertions Migration Guide</a>.</p>
<p>There are several ways to perform the conversion :</p>
<ul>
<li><a href="#automatic-conversion">automatically</a> with a shell script.</span></li>
<li><a href="#manual-conversion">manually</a> using the regexps described in this page.</li>
<li>With IntelliJ IDEA, using the <a href="https://plugins.jetbrains.com/plugin/10345-assertions2assertj">Assertions2Assertj</a> plugin.</li>
<li>With IntelliJ IDEA, using the Structural Search and Replace (SSR) feature.</li>
</ul>
<h3 class="page-header"><span id="automatic-conversion"></span>Automatically converting your JUnit assertions to AssertJ</h3>
<p>It is simple : use the proper script depending on your OS :</p>
<ul>
<li>unix and windows : <a href="https://github.com/joel-costigliola/assertj-core/blob/master/src/main/scripts/convert-junit-assertions-to-assertj.sh">convert-junit-assertions-to-assertj.sh</a></span></li>
<li>osx : <a href="https://github.com/joel-costigliola/assertj-core/blob/master/src/main/scripts/convert-junit-assertions-to-assertj-on-osx.sh">convert-junit-assertions-to-assertj-on-osx.sh</a></span></li>
</ul>
<p>Each shell scripts is based on the <span class="small-code">sed</span> stream editor and regexps. It recursively looks at all <span class="small-code">*Test.java</span> files and performs search and replace to convert JUnit assertions to AssertJ ones (if the <span class="small-code">*Test.java</span> file pattern does not suit you, just change the script according to your needs).</p>
<p>The script works on windows within a bash console like git bash (tested) or cygwin (not tested). I haven't tested the OSX script since I don't own a mac.</p>
<p>Usage : On windows or unix execute the script in the base directory containing the test files :</p>
<pre><code class="bash"># in the directory containing the test files
convert-junit-assertions-to-assertj.sh</code></pre>
<p>Usage : On OSX execute the script in the base directory containing the test files :</p>
<pre><code class="bash"># in the directory containing the test files
convert-junit-assertions-to-assertj-on-osx.sh</code></pre>
<p>It is not perfect but should do most of the job. After executing it, you will need to : </p>
<ul>
<li>optimize imports with your IDE to remove unused imports.</span></li>
<li>if you were using <span class="small-code">assertEquals</span> with a delta to compare numbers, you will need to statically import <span class="small-code">org.assertj.core.api.Assertions.offset</span> which is how you express deltas in AssertJ (see the <a href="https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java">number_assertions_with_offset_examples()</a> test at the end of NumberAssertionsExamples for an example).</li>
</ul>
<p>The script handles the cases where you use an assertion description, for example :<pre><code class="java">assertEquals("test context", "a", "a");</code></pre> will be replaced by :</p><p><pre><code class="java">assertThat("a").as("test context").isEqualTo("a");</code></pre> </p>
<p>See below the output of the script execution :</p>
<pre><code class="diff" style="font-size:83%;">Converting JUnit assertions to AssertJ assertions on files matching pattern : *Test.java
1 - Replacing : assertEquals(0, myList.size()) ............... by : assertThat(myList).isEmpty()
2 - Replacing : assertEquals(expectedSize, myList.size()) .... by : assertThat(myList).hasSize(expectedSize)
3 - Replacing : assertEquals(expectedDouble, actual, delta) .. by : assertThat(actual).isCloseTo(expectedDouble, within(delta))
4 - Replacing : assertEquals(expected, actual) ............... by : assertThat(actual).isEqualTo(expected)
5 - Replacing : assertArrayEquals(expectedArray, actual) ..... by : assertThat(actual).isEqualTo(expectedArray)
6 - Replacing : assertNull(actual) ........................... by : assertThat(actual).isNull()
7 - Replacing : assertNotNull(actual) ........................ by : assertThat(actual).isNotNull()
8 - Replacing : assertTrue(logicalCondition) ................. by : assertThat(logicalCondition).isTrue()
9 - Replacing : assertFalse(logicalCondition) ................ by : assertThat(logicalCondition).isFalse()
10 - Replacing : assertSame(expected, actual) ................. by : assertThat(actual).isSameAs(expected)
11 - Replacing : assertNotSame(expected, actual) .............. by : assertThat(actual).isNotSameAs(expected)
12 - Replacing JUnit static imports by AssertJ ones, at this point you will probably need to :
12 --- optimize imports with your IDE to remove unused imports
12 --- add "import static org.assertj.core.api.Assertions.within;" if you were using JUnit number assertions with deltas</code></pre>
<p>On windows you might have permission problems as sed creates temp files. If you get this error, you have to grant write permissions to the current user for the folder where your test files live.</p>
<pre><code class="diff" style="font-size:83%;">Converting JUnit assertions to AssertJ assertions on files matching pattern : *Test.java
1 - Replacing : assertEquals(0, myList.size()) ................. by : assertThat(myList).isEmpty()
sed: preserving permissions for `./sed005904': Permission denied
sed: preserving permissions for `./sed004220': Permission denied</code></pre>
<h3 class="page-header"><span id="manual-conversion"></span>Converting your JUnit assertions to AssertJ with regexp</h3>
<p>Here's a list of find/replace expressions that allow to change JUnit assertions into AssertJ assertions (don't forget to check the regexp mode in your editor replace window).</p>
<p>The order of find/replace is important, so that you can benefit from the most relevant AssertJ assertions. For example you should convert <span class="small-code">assertEquals(0, myList.size())</span> to <span class="small-code">assertThat(myList).isEmpty()</span> instead of
<span class="small-code">assertThat(myList.size()).isEqualTo(0)</span>
</p>
<h5 class="page-header">1 - Converting <span class="small-code">assertEquals(0, myList.size())</span> to <span class="small-code">assertThat(myList).isEmpty()</span></h5>
<p>Find/replace Regexp :</p>
<pre><code class="java">assertEquals\(0,(.*).size\(\)\); -> assertThat(\1).isEmpty();</code></pre>
<p>It's important to run this one before the <span class="small-code">assertEquals</span> -> <span class="small-code">isEqualTo</span> conversion to avoid ending with : <span class="small-code">assertThat(myList.size()).isEqualTo(0)</span></p>
<h5 class="page-header">2 - Converting <span class="small-code">assertEquals(expectedSize, myList.size())</span> to <span class="small-code">assertThat(myList).hasSize(expectedSize)</h5>
<p>Find/replace Regexp :</p>
<pre><code class="java">assertEquals\((.*),(.*).size\(\)\); -> assertThat(\2).hasSize(\1);</code></pre>
<p>It's important to run this one before the <span class="small-code">assertEquals</span> -> <span class="small-code">isEqualTo</span> conversion to avoid ending with : <span class="small-code">assertThat(myList.size()).isEqualTo(expectedSize)</span></p>
<h5 class="page-header">3 - Converting <span class="small-code">assertEquals(value, valueUnderTest)</span> to <span class="small-code">assertThat(valueUnderTest).isEqualTo(value)</h5>
<p>Find/replace Regexp :</p>
<pre><code class="java">assertEquals\((.*),(.*)\); -> assertThat(\2).isEqualTo(\1);</code></pre>
<h5 class="page-header">4 - Converting <span class="small-code">assertNull(objectUnderTest)</span> to <span class="small-code">assertThat(objectUnderTest).isNull()</h5>
<p>Find/replace Regexp :</p><pre><code class="java">assertNull\((.*)\); -> assertThat(\1).isNull();</code></pre>
<h5 class="page-header">5 - Converting <span class="small-code">assertNotNull(objectUnderTest)</span> to <span class="small-code">assertThat(objectUnderTest).isNotNull()</h5>
<p>Find/replace Regexp :</p>
<pre><code class="java">assertNotNull\((.*)\); -> assertThat(\1).isNotNull();</code></pre>
<h5 class="page-header">6 - Converting <span class="small-code">assertTrue(logicalCondition)</span> to <span class="small-code">assertThat(logicalCondition).isTrue()</h5>
<p>Find/replace Regexp :</p>
<pre><code class="java">assertTrue\((.*)\); -> assertThat(\1).isTrue();</code></pre>
<h5 class="page-header">7 - Converting <span class="small-code">assertFalse(logicalCondition)</span> to <span class="small-code">assertThat(logicalCondition).isFalse()</h5>
<p>Find/replace Regexp :</p>
<pre><code class="java">assertFalse\((.*)\); -> assertThat(\1).isFalse();</code></pre>
</div>
</div>
</div>
<br>
<!--
<div class="container">
<footer>
<div class="row">
<div class="col-lg-12">
<p>AssertJ - Licensed under the Apache License, Version 2.0.</p>
</div>
</div>
</footer>
</div>
-->
<script src="js/jquery-1.10.2.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/modern-business.js"></script>
<script src="js/assertj.js"></script>
</body>
</html>