-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathassertj-core-migrating-from-fest.html
231 lines (182 loc) · 14.7 KB
/
assertj-core-migrating-from-fest.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
223
224
225
226
227
228
229
230
231
<!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">Migrating from Fest 2.x</h1>
<p>Migrating from Fest 2.x is easy since AssertJ is a fork of Fest and there are very few breaking changes.</p>
<p>First thing first, change your static import, from:</p>
<pre><code class="java">import static org.fest.assertions.api.Assertions</code></pre>
<p>to</p>
<pre><code class="java">import static org.assertj.core.api.Assertions</code></pre>
<p>For most users, that should be all you need to do !</p>
<p>For linux users, a quick way to do that is to use the following command:</p>
<pre><code class="bash">find . -name "*.java" -exec sed -i "s/org.fest.assertions.api.Assertions/org.assertj.core.api.Assertions/g" '{}' \;</code></pre>
<p>For mac users, you have an almost identical command (notice the "" after -i) :</p>
<pre><code class="bash">find . -name "*.java" -exec sed -i "" "s/org.fest.assertions.api.Assertions/org.assertj.core.api.Assertions/g" '{}' \;</code></pre>
<p>To do even more in a single command and also replacing non static imports and several statements, linux users can use the following command instead:</p>
<pre><code class="bash">find . -name "*.java" -exec sed -i "s/import static org.fest.assertions.api./import static org.assertj.core.api./g;s/import org.fest.assertions.api./import org.assertj.core.api./g;s/import org.fest.assertions.core.Condition;/import org.assertj.core.api.Condition;/g;s/import org.fest.util./import org.assertj.core.util./g;s/import org.fest.assertions.data./import org.assertj.core.data./g" '{}' \;</code></pre>
<p>Again, the equivalent command for Mac users is:</p>
<pre><code class="bash">find . -name "*.java" -exec sed -i "" "s/import static org.fest.assertions.api./import static org.assertj.core.api./g;s/import org.fest.assertions.api./import org.assertj.core.api./g;s/import org.fest.assertions.core.Condition;/import org.assertj.core.api.Condition;/g;s/import org.fest.util./import org.assertj.core.util./g;s/import org.fest.assertions.data./import org.assertj.core.data./g" '{}' \;</code></pre>
<p><strong>Note:</strong> if your project uses both FestAssert Core assertions and Fest Joda-Time assertions, execute the above scripts for FestAssert Core Assertions <strong>after</strong> <a href="assertj-joda-time.html#migrating-from-fest">those for for Joda-Time</a>!</p>
<h5>Renamed assertions since the Fest fork :</h5>
<ul>
<li><span class="small-code">isLenientEqualsToByIgnoringFields</span> renamed to <span class="small-code">isEqualToIgnoringGivenFields</span></li>
<li><span class="small-code">isLenientEqualsToByAcceptingFields</span> renamed to <span class="small-code">isEqualToComparingOnlyGivenFields</span></li>
<li><span class="small-code">isLenientEqualsToByIgnoringNullFields</span> renamed to <span class="small-code">isEqualToIgnoringNullFields</span></li>
<li><span class="small-code">isEqualsToByComparingFields</span> renamed to <span class="small-code">isEqualToComparingFieldByField</span></li>
</ul>
<p>The linux commands to automatically rename the assertions:</p>
<pre><code class="bash">find . -name "*.java" -exec sed -i "s/isLenientEqualsToByIgnoringFields/isEqualToIgnoringGivenFields/g;s/isLenientEqualsToByAcceptingFields/isEqualToComparingOnlyGivenFields/g;s/isLenientEqualsToByIgnoringNullFields/isEqualToIgnoringNullFields/g;s/isEqualsToByComparingFields/isEqualToComparingFieldByField/g" '{}' \;</code></pre>
<p>And the commands for Mac users:</p>
<pre><code class="bash">find . -name "*.java" -exec sed -i "" "s/isLenientEqualsToByIgnoringFields/isEqualToIgnoringGivenFields/g;s/isLenientEqualsToByAcceptingFields/isEqualToComparingOnlyGivenFields/g;s/isLenientEqualsToByIgnoringNullFields/isEqualToIgnoringNullFields/g;s/isEqualsToByComparingFields/isEqualToComparingFieldByField/g" '{}' \;</code></pre>
<h5>Removed assertions since the Fest fork (we thought that they were very unclear) :</h5>
<ul>
<li><span class="small-code">areNotAtLeast</span>, <span class="small-code">areNotAtMost</span>, <span class="small-code">areNotExactly</span></li>
<li><span class="small-code">haveNotAtLeast</span>, <span class="small-code">haveNotAtMost</span>, <span class="small-code">haveNotExactly</span></li>
</ul>
<p>If something's missing or doesn't work, please <a href="https://github.com/joel-costigliola/assertj-core/issues?state=open">create an issue</a> to update this section.</p>
<h1 class="page-header"><span id="fest-1.4" class="adjustAnchor"></span>Migrating from Fest 1.4</h1>
<p>Migrating Fest Assert 1.4 is a little more involved than migrating from Fest Assert 2.x but is not difficult.</p>
<h5>Major changes</h5>
<ul>
<li>the package name changed, API is now in <span class="small-code">org.assertj.core.api</span></li>
<li>the <span class="small-code">onProperty</span> feature has been renamed to the more powerful <span class="small-code">extracting</span></li>
</ul>
<h5>Minor changes</h5>
<ul>
<li><span class="small-code">IteratorAssert</span> has been replaced by <span class="small-code">IterableAssert</span></li>
<li><span class="small-code">satisfies()</span> method has been replaced by <span class="small-code">is()</span> or <span class="small-code">has()</span> (hint: use the more readable for your code)</li>
<li><span class="small-code">Delta.delta()</span> becomes <span class="small-code">offset()</span>, it is directly available from <span class="small-code">Assertions.offset()</span></li>
<li><span class="small-code">fail()</span> method has been removed - prefer <span class="small-code">failBecauseExceptionWasNotThrown(exception class)</span>.</li>
<li>make your own assertion inherit from <span class="small-code">AbstractAssert</span> (more details below)</li>
<li><span class="small-code">description()</span> becomes <span class="small-code">descriptionText()</span></li>
<li><span class="small-code">excludes()</span> becomes <span class="small-code">doesNotContain()</span></li>
</ul>
<h3 class="page-header">Migration detailed steps</h3>
<ul>
<li><a href="#migrating-custom-assertions">Migrating custom assertions</a></li>
<li><a href="#failure-to-fail"><span class="small-code">failure(String message)</span> replaced by <span class="small-code">fail(String message)</span></a></li>
<li><a href="#fail-no-params-removed"><span class="small-code">fail()</span> method removed</a></li>
<li><a href="#onProperty-to-extracting">Migrating <span class="small-code">onProperty</span> usage to <span class="small-code">extracting</span></a></li>
</ul>
<h4 class="page-header"><span id="migrating-custom-assertions" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> Migrating custom assertions</h4>
<p>It is simple, instead of inheriting from <span class="small-code">GenericAssert</span> you inherit from <span class="small-code">AbstractAssert</span> and invert the parameter order in the constructor.</p>
<p>in Fest 1.4 :</p>
<pre><code class="java">public DateTimeAssert(DateTime actual) {
super(DateTimeAssert.class, actual);
}</code></pre>
<p>in AssertJ :</p>
<pre><code class="java">public DateTimeAssert(DateTime actual) {
super(actual, DateTimeAssert.class);
}</code></pre>
<h4 class="page-header"><span id="failure-to-fail" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> <span class="small-code">failure(String message)</span> replaced by <span class="small-code">fail(String message)</span></h4>
<p>In Fest 1.4 :</p>
<pre><code class="java">throw failure(concat("expecting ", objectName, " value not to be null"));</code></pre>
<p>In AssertJ :</p>
<pre><code class="java">Fail.fail(String.format("expecting %s value not to be null", objectName));</code></pre>
<h4 class="page-header"><span id="fail-no-params-removed" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> <span class="small-code">fail()</span> method removed</h4>
<p>We think that when using <span class="small-code">fail</span> in a test, one should always set a meaningful failure message, thus the removing of no arg <span class="small-code">fail()</span> method.
Since <span class="small-code">fail()</span> was mainly used when expecting an exception, you can replace it by : <span class="small-code">failBecauseExceptionWasNotThrown(Exception class)</span>.
</p>
<p>For example, executing :</p>
<pre><code class="java">failBecauseExceptionWasNotThrown(FileNotFoundException.class)</code></pre>
<p>will throw an <span class="small-code">AssertionError</span> with message "Expected FileNotFoundException to be thrown"</p>
<p>All <span class="small-code">fail</span> methods are available from the <span class="small-code">Assertions</span> or the <span class="small-code">Fail</span> classes.</p>
<h4 class="page-header"><span id="onProperty-to-extracting" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> Migrating <span class="small-code">onProperty</span> usage to <span class="small-code">extracting</span></h4>
<p>In FEST 1.4 :</p>
<pre><code class="java">assertThat(fellowshipOfTheRing).onProperty("name").contains("Gandalf", "Frodo", "Legolas");</code></pre>
<p>In AssertJ :</p>
<pre><code class="java">assertThat(fellowshipOfTheRing).extracting("name").contains("Gandalf", "Frodo", "Legolas");</code></pre>
<p>Moreover <span class="small-code">extracting</span> is able to extract both properties and fields while Fest can only extract properties.</p>
</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>