-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathassertj-guava.html
453 lines (343 loc) · 22.6 KB
/
assertj-guava.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<!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"></li>
<li><a href="assertj-guava.html">Overview</a></li>
<li><a href="assertj-guava.html#quickstart">Quick start</a></li>
<li><a href="assertj-guava.html#latest-release">Releases</a></li>
<li><a href="http://joel-costigliola.github.io/assertj/guava/api/index.html" target="_blank">Javadoc</a></li>
<li><a href="assertj-guava.html#migrating-from-fest">Migrating from Fest</a></li>
<li><a href="assertj-guava.html#mailing-list">Mailing list</a></li>
<li><a href="assertj-guava.html#code">Code & issues <i class="fa fa-github"></i></a></li>
<li><a href="assertj-guava.html#contributing">Contributing</a></li>
</ul>
</div>
</div>
<div class="col-lg-10 col-md-10 col-sm-10 text-left">
<h1 class="page-header">AssertJ Guava assertions</h1>
<p>AssertJ assertions for <a href="https://github.com/google/guava">Guava</a> provides assertions for Guava types like <span class="small-code">Multimap</span>, <span class="small-code">Multiset</span>, <span class="small-code">Table</span>, <span class="small-code">Optional</span>, <span class="small-code">Range</span> or <span class="small-code">ByteSource</span>.</p>
<p>If you think some assertion is missing, please <a href="https://github.com/joel-costigliola/assertj-guava/issues">create an issue</a> or even better contribute!</p>
<p>AssertJ Guava is hosted on github : <a href="https://github.com/joel-costigliola/assertj-guava">https://github.com/joel-costigliola/assertj-guava</a>.</p>
<h3 class="page-header"><span id="quickstart"></span>Guava assertions quickstart guide</h3>
<p>To quickly start using Guava assertions, follow the steps below.</p>
<h4 class="page-header">1 - Add assertj-guava dependency to your project</h4>
<div class="row">
<div class="col-md-6 portfolio-item">
<h3 class="quickstart">Maven</h3>
<p>AssertJ artifacts are in the Maven central repository.</p>
<pre><code class="xml"><dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-guava</artifactId>
<!-- Use 2.x version if you rely on Java 7 / AssertJ Core 2.x -->
<version>3.3.0</version>
<scope>test</scope>
</dependency></code></pre>
</div>
<div class="col-md-6 portfolio-item">
<h3 class="quickstart">Gradle</h3>
<p>For Gradle users (using the Maven Central Repository)</p>
<pre><code class="java">testCompile("org.assertj:assertj-guava:3.2.0")</code></pre>
<p>Or version 2.0.1 for Java 7 projects</p>
<pre><code class="java">testCompile("org.assertj:assertj-guava:2.0.1")</code></pre>
</div>
<div class="col-md-12">
<p>If you use another dependency tool, please check this page to find the relevant <a href="http://search.maven.org/#artifactdetails|org.assertj|assertj-guava|3.2.0|bundle">assertj-guava dependency declaration</a> for your tool.</p>
</div>
</div>
<h4 class="page-header">2 - Statically import <span class="small-code">org.assertj.guava.api.Assertions.assertThat</span> ...</h4>
<p>... and use your preferred IDE code completion after <span class="small-code">assertThat.</span></p>
<p>Example :</p>
<pre><code class="java">import static org.assertj.guava.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.entry;
// Multimap assertions
Multimap<String, String> actual = ArrayListMultimap.create();
actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
assertThat(actual).containsKeys("Lakers", "Spurs");
assertThat(actual).contains(entry("Lakers", "Kobe Bryant"), entry("Spurs", "Tim Duncan"));
// Multiset assertions
Multiset<String> actual = HashMultiset.create();
actual.add("shoes", 2);
assertThat(actual).contains(2, "shoes");
assertThat(actual).containsAtLeast(1, "shoes");
assertThat(actual).containsAtMost(3, "shoes");
// Range assertions
Range<Integer> range = Range.closed(10, 12);
assertThat(range).isNotEmpty()
.contains(10, 11, 12)
.hasClosedLowerBound()
.hasLowerEndpointEqualTo(10)
.hasUpperEndpointEqualTo(12);
// Table assertions
Table<Integer, String, String> bestMovies = HashBasedTable.create();
bestMovies.put(1970, "Palme d'Or", "M.A.S.H");
bestMovies.put(1994, "Palme d'Or", "Pulp Fiction");
bestMovies.put(2008, "Palme d'Or", "Entre les murs");
bestMovies.put(2000, "Best picture Oscar", "American Beauty");
bestMovies.put(2011, "Goldener Bär", "A Separation");
assertThat(bestMovies).hasRowCount(5).hasColumnCount(3).hasSize(5)
.containsValues("American Beauty", "A Separation", "Pulp Fiction")
.containsCell(1994, "Palme d'Or", "Pulp Fiction")
.containsColumns("Palme d'Or", "Best picture Oscar", "Goldener Bär")
.containsRows(1970, 1994, 2000, 2008, 2011);
// Optional assertions
Optional<String> optional = Optional.of("Test");
assertThat(optional).isPresent().contains("Test");</code></pre>
<p><span class="small-code">assertThat</span> and <span class="small-code">entry</span> are static imports from the <span class="small-code">Assertions</span> class.</p>
<p>Note that you can find more working examples in the <a href="https://github.com/joel-costigliola/assertj-examples/blob/master/assertions-examples/src/test/java/org/assertj/examples/guava">guava package</a> of the <a href="https://github.com/joel-costigliola/assertj-examples/">assertj-examples</a> project.</p>
<h3 class="page-header"><span id="3.3.0"></span><span id="latest-release"></span>Guava assertions 3.3.0 release</h3>
<p>The release notes are published in the <a href="https://assertj.github.io/doc/#assertj-guava">new website</a>.</p>
<h3 class="page-header"><span id="3.2.1"></span></span>Guava assertions 3.2.1 release</h3>
<p>Release date : 2018-05-05</p>
<p>AssertJ Guava 3.2.1 requires Java 8 and AssertJ Core 3.11.1</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Replace assertj-core version range dependency by a fixed version</li>
<li>Javadoc improvements</li>
</ul>
<h3 class="page-header"><span id="3.2.0"></span>Guava assertions 3.2.0 release</h3>
<p>Release date : 2018-04-30</p>
<p>AssertJ Guava 3.2.0 requires Java 8 and AssertJ Core 3.x.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Use Guava 21+ which is java 8 compatible</li>
<li>Can be used with assertj-core 3.9.1+</li>
<li>Set Guava dependency as provided</li>
<li>Javadoc improvements</li>
</ul>
<h3 class="page-header"><span id="2.0.1"/></span></span>Guava assertions 2.0.1 release</h3>
<p>Release date : 2018-05-05</p>
<p>AssertJ Guava 2.0.1 requires Java 7 and AssertJ Core 2.x.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Cap Guava version to 20.0 as 21 requires java 8</li>
<li>Javadoc improvements</li>
</ul>
<h3 class="page-header"><span id="3.1.0"/></span><span id="latest-release"/></span>Guava assertions 3.1.0 release</h3>
<p>Release date : 2016-10-24</p>
<p>AssertJ Guava 3.1.0 requires Java 8 and AssertJ Core 3.x.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Add <a href="assertj-guava.html#3.1.0-multiset"><span class="small-code">Multiset</span> assertions</a>. <span class="contributor">(Max Daniline)</span></li>
<li>Don't impose a version range on Guava dependency.</li>
</ul>
<h4 class="page-header"><span id="3.1.0-multiset" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> Multiset assertions.</h4>
<p>Allow to check the content of Multisets.</p>
<p>Example :</p>
<pre><code class="java"> Multiset<String> actual = HashMultiset.create();
actual.add("shoes", 2);
assertThat(actual).contains(2, "shoes");
assertThat(actual).containsAtLeast(1, "shoes");
assertThat(actual).containsAtMost(3, "shoes");</code></pre>
<h3 class="page-header"><span id="3.0.0"/></span>Guava assertions 3.0.0 release</h3>
<p>Release date : 2015-12-19</p>
<p>AssertJ Guava 3.0.0 requires Java 8 and AssertJ Core 3.x.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li><b>Breaking change</b> : AssertJ Guava 3.0.0 relies on Java 8 and Assertj Core 3.x .</li>
<li>Improve OSGi manifest.</li>
</ul>
<h3 class="page-header"><span id="2.0.0"/></span></span>Guava assertions 2.0.0 release</h3>
<p>Release date : 2015-07-21</p>
<p>Thanks to <span class="contributor">William Delanoue</span> and <span class="contributor">Pascal Schumacher</span> for their contributions to this version.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li><b>Breaking change</b> : AssertJ Guava 2.0.0 relies on Java 7 and Assertj Core 2.x (but not 3.x).</li>
<li>New Multimap assertion : <a href="assertj-guava.html#2.0.0-hasSameEntriesAs"><span class="small-code">hasSameEntriesAs(Multimap other)</span></a>.</li>
<li>New Multimap assertion : <a href="assertj-guava.html#2.0.0-containsAllEntriesOf"><span class="small-code">containsAllEntriesOf(Multimap other)</span></a>.</li>
<li>Allow to run assertions on Optional value with <a href="assertj-guava.html#2.0.0-extractingValue"><span class="small-code">extractingValue()</span></a>. <span class="contributor">(William Delanoue)</span></li>
<li>Make <span class="small-code">MapEntry</span> generic i.e. : <span class="small-code">MapEntry<K, V></span>.</li>
<li>Make assertj-guava OSGI compatible.</li>
<li>Javadoc improvements. <span class="contributor">(Pascal Schumacher)</span></li>
</ul>
<h4 class="page-header"><span id="2.0.0-hasSameEntriesAs" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> New Multimap assertion : <span class="small-code">hasSameEntriesAs(Multimap other)</span>.</h4>
<p>Allow to compare the content of two multimaps who would not be equal because being of different types like <span class="small-code">SetMultimap</span> and <span class="small-code">ListMultimap</span>.</p>
<p>Example :</p>
<pre><code class="java">Multimap<String, String> actual = ArrayListMultimap.create();
listMultimap.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
listMultimap.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
Multimap<String, String> setMultimap = TreeMultimap.create();
setMultimap.putAll("Spurs", newHashSet("Tony Parker", "Tim Duncan", "Manu Ginobili"));
setMultimap.putAll("Bulls", newHashSet("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
// assertion will pass as listMultimap and setMultimap have the same content
assertThat(listMultimap).hasSameEntriesAs(setMultimap);
// this assertion FAILS even though both multimaps have the same content
assertThat(listMultimap).isEqualTo(setMultimap);</code></pre>
<h4 class="page-header"><span id="2.0.0-containsAllEntriesOf" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> New Multimap assertion : <span class="small-code">containsAllEntriesOf(Multimap other)</span>.</h4>
<p>Verifies that the actual <span class="small-code">Multimap</span> contains all entries of the given one (it might contain more entries).</p>
<p>Example :</p>
<pre><code class="java">Multimap<String, String> actual = ArrayListMultimap.create();
actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
actual.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));
Multimap<String, String> other = TreeMultimap.create();
other.putAll("Spurs", newHashSet("Tony Parker", "Tim Duncan"));
other.putAll("Bulls", newHashSet("Michael Jordan", "Scottie Pippen"));
// assertion will pass as other is a subset of actual.
assertThat(actual).containsAllEntriesOf(other);
// this assertion FAILS as other does not contain "Spurs -> "Manu Ginobili" and "Bulls" -> "Derrick Rose"
assertThat(other).containsAllEntriesOf(actual);</code></pre>
<h4 class="page-header"><span id="2.0.0-extractingValue" class="adjustAnchor"></span><i class="fa fa-arrow-circle-right"></i> Allow to run assertions on Optional value with <span class="small-code">extractingValue()</span>.</h4>
<p>Allow to chain assertion on the content of the <span class="small-code">Optional</span> using <span class="small-code">extractingValue()</span>.</p>
<p>Example :</p>
<pre><code class="java">Optional<Number> optional = Optional.of(12L);
assertThat(optional).extractingValue()
.isInstanceOf(Long.class)
.isEqualTo(12L);
</code></pre>
<p>There is a special support for <span class="small-code">String</span> values with <span class="small-code">extractingCharSequence()</span>.</p>
<p>Example :</p>
<pre><code class="java">Optional<String> optional = Optional.of("Bill");
// extractingCharSequence allows to chain String specific assertions
assertThat(optional).extractingCharSequence()
.startsWith("Bi");</code></pre>
<h3 class="page-header"><span id="1.3.1"/></span></span>Guava assertions 1.3.1 release</h3>
<p>Release date : 2015-01-21</p>
<p>Bugfix release fixing <span class="small-code">hasUpperEndpointEqual</span> assertion error message (thanks <span class="contributor">László Balázs-Csíki</span> for reporting it).</p>
<h3 class="page-header"><span id="1.3.0"/></span>Guava assertions 1.3.0 release</h3>
<p>Release date : 2014-10-27</p>
<p>Thanks to <span class="contributor">Simon Barbey</span> and <span class="contributor">Jochen Kupperschmidt</span> for their contributions to this version.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>New Multimap assertion : <span class="small-code">isNotEmpty()</span>. <span class="contributor">(Simon Barbey)</span></li>
<li>Javadoc code syntax highlighting.</li>
<li>Javadoc fix. <span class="contributor">(Jochen Kupperschmidt)</span></li>
</ul>
<h3 class="page-header"><span id="1.2.0"/></span><span id="1.2.0"/></span>Guava assertions 1.2.0 release</h3>
<p>Release date : 2014-05-25</p>
<p>Thanks to <span class="contributor">David Harris</span> and <span class="contributor">Marcin Kwaczyński</span> for their contributions to this version.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Guava Range assertions <span class="contributor">(Marcin Kwaczyński)</span></li>
<li>New Table assertions <span class="contributor">(David Harris)</span></li>
<li>Javadoc improvements</li>
</ul>
<h3 class="page-header"><span id="1.1.0"/></span>Guava assertions 1.1.0 release</h3>
<p>Release date : 2013-12-27</p>
<p>Thanks to <span class="contributor">Andrew Gaul</span> for his contributions to this version.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Guava ByteSource assertions <span class="contributor">(Andrew Gaul)</span></li>
<li>Javadoc improvements.</li>
</ul>
<h3 class="page-header">Guava assertions 1.0.0 release</h3>
<p>2013-03-26 : 1.0.0 release</p>
<p>First release after Fest fork.</p>
<p>Thanks to <span class="contributor">Jan Gorman</span> for his contributions to this version.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Guava Table assertions <span class="contributor">(Jan Gorman)</span></li>
</ul>
<h3 class="page-header"><span id="javadoc"/></span>Javadoc of latest release</h3>
<p>Latest javadoc release : <a href="http://joel-costigliola.github.io/assertj/guava/api/index.html">AssertJ Guava javadoc</a>.</p>
<h3 class="page-header"><span id="migrating-from-fest"/></span>Migrating from Fest Guava assertions</h3>
<p>Migrating from Fest Guava to AssertJ Guava Assertions is easy, you only have to change your static import, just replace :
</p>
<pre><code class="java">import static org.fest.assertions.api.GUAVA</code></pre>
<p>with :</p>
<pre><code class="java">import static org.assertj.guava.api.Assertions</code></pre>
<h3 class="page-header"><span id="core-and-guava-assertions"/></span>Using both AssertJ <a href="https://github.com/joel-costigliola/assertj-core">Core assertions</a> and Guava assertions</h3>
<p>You will have to use two static imports : one for <span class="small-code">org.assertj.core.api.Assertions.assertThat</span>
to get core assertions and one <span class="small-code">org.assertj.guava.api.Assertions.assertThat</span> for Guava assertions.</p>
<pre><code class="java">import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
...
// assertThat comes from org.assertj.guava.api.Assertions.assertThat static import
Multimap<String, String> actual = ArrayListMultimap.create();
actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
assertThat(actual).hasSize(6);
assertThat(actual).containsKeys("Lakers", "Spurs");
// assertThat comes from org.assertj.core.api.Assertions.assertThat static import
assertThat("hello world").startsWith("hello");</code></pre>
<h3 class="page-header"><span id="mailing-list"/></span>Mailing list</h3>
<p>If you have any questions, please use
<a href="https://groups.google.com/forum/?fromgroups=#!forum/assertj">AssertJ google group</a>.</p>
<h3 class="page-header"><span id="code"/></span>Code and issue tracker</h3>
<p>AssertJ Guava is hosted on github : <a href="https://github.com/joel-costigliola/assertj-guava">https://github.com/joel-costigliola/assertj-guava</a>.</p>
<p>Please report bugs or missing features in <a href="https://github.com/joel-costigliola/assertj-guava/issues?labels=&milestone=&page=1&state=open">AssertJ Guava issue tracker</a>.</p>
<h3 class="page-header"><span id="contributing"/></span>Contributing</h3>
<p>Thanks for your interest ! Please check our <a href="https://github.com/joel-costigliola/assertj-guava/blob/master/CONTRIBUTING.md#contributing">contributor's guidelines</a>.</p>
<p>Special thanks to AssertJ Guava contributors:</p>
<ul>
<li>Florent Biville</li>
<li>Max Daniline</li>
<li>William Delanoue</li>
<li>Frédéric Marcel Falliere</li>
<li>Andrew Gaul</li>
<li>Jan Gorman</li>
<li>Kornel Kiełczewski</li>
<li>David Harris</li>
<li>Marcin Kwaczyński</li>
<li>Pascal Schumacher</li>
</ul>
</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>