-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathassertj-assertions-generator.html
854 lines (731 loc) · 45.7 KB
/
assertj-assertions-generator.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
<!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">Assertions generator</li>
<li><a href="assertj-assertions-generator.html">Overview</a></li>
<li><a href="assertj-assertions-generator.html#philosophy">Philosophy</a></li>
<li><a href="assertj-assertions-generator.html#generated-assertions">Created assertions</a></li>
<li><a href="assertj-assertions-generator.html#generated-entry-points">Created entry points</a></li>
<li><a href="assertj-assertions-generator.html#generated-assertions-templates">Assertion templates</a></li>
<li><a href="assertj-assertions-generator.html#quickstart">Quick start</a></li>
<li><a href="assertj-assertions-generator.html#news">News & releases</a></li>
<li><a href="assertj-assertions-generator.html#mailing-list">Mailing list</a></li>
<li><a href="assertj-assertions-generator.html#code">Code & issues <i class="fa fa-github"></i></a></li>
<li><a href="assertj-assertions-generator.html#contributing">Contributing</a></li>
<li class="sidenav-header">Maven plugin</li>
<li><a href="assertj-assertions-generator-maven-plugin.html">Overview</a></li>
<li><a href="assertj-assertions-generator-maven-plugin.html#quickstart">Quick start</a></li>
<li><a href="assertj-assertions-generator-maven-plugin.html#configuration">Configuration</a></li>
<li><a href="assertj-assertions-generator-maven-plugin.html#custom-templates">Custom templates</a></li>
<li><a href="assertj-assertions-generator-maven-plugin.html#news">News & releases</a></li>
<li><a href="assertj-assertions-generator-maven-plugin.html#mailing-list">Mailing list</a></li>
<li><a href="assertj-assertions-generator-maven-plugin.html#code">Code & issues <i class="fa fa-github"></i></a></li>
<li><a href="assertj-assertions-generator-maven-plugin.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 Assertions Generator</h1>
<p>Use the Assertion Generator to create assertions specific to your own classes, either through a simple <a href="#quickstart">command line tool</a>, a <a href="assertj-assertions-generator-maven-plugin.html">maven plugin</a> or a <a href="https://github.com/assertj/assertj-generator-gradle-plugin">gradle plugin</a>.</p>
<p>Having assertions specific to your own classes allows you to use the domain model vocabulary in your test assertions, making them easier to read and closer to the problem you are trying to solve. It's a way to apply Domain Driven Design ubiquitous language in tests.</p>
<p>Let's say you have a <span class="small-code">Player</span> class with <span class="small-code">name</span>, <span class="small-code">team</span> and <span class="small-code">teamMates</span> properties (also works with public fields).</p>
<pre><code class="java">public class Player {
private String name;
private String team;
private List<String> teamMates;
// constructor and setters omitted
public String getName() {
return name;
}
public String getTeam() {
return team;
}
public List<String> getTeamMates() {
return teamMates;
}
@Override
public String toString() {
return "Player[name="+name+"]";
}
}</code></pre>
<p>The generator is able to create a <span class="small-code">PlayerAssert</span> assertion class with a
<span class="small-code">hasName</span> and a <span class="small-code">hasTeam</span> assertion which can be used like this :</p>
<pre><code class="java">assertThat(mj).hasName("Michael Jordan")
.hasTeam("Chicago Bulls")
.hasTeamMates("Scottie Pippen", "Tony Kukoc");</code></pre>
<p>The error message of a failed assertion reflects the property checked. For example if <span class="small-code">hasName</span> fails you will get this error message :</p>
<pre><code class="java">Expecting name of:
<Player[name=Air Jordan]>
to be:
<Michael Jordan>
but was:
<Air Jordan></code></pre>
<h3 class="page-header"><span id="philosophy"></span>Philosophy</h3>
<p>The goal of the generator is to lower the barrier to use custom assertions. In our opinion it is just a starting point to get domain specific assertions. Some people will be satisfied with the generated code, other will modify it to get better wording or additional assertions and put the code in source control as test utilities classes.</p>
<p>The generator has its <a href="#limitations">limitations</a> and is sometimes not able to generate code that is 100% compilation errors free (but don't worry most of the time it does !). We don't see this as a blocker because if 99% of the generated code is correct and the remaining is easy to fix, then we consider the goal to be achieved since you were able to get a lot custom assertions with limited effort. If you need 100% correct generated code then you can exclude the offending classes from having assertions being generated or even better <a href="https://github.com/joel-costigliola/assertj-assertions-generator/issues?labels=&milestone=&page=1&state=open">contribute</a> to fix the generator !</p>
<h3 class="page-header"><span id="generated-assertions"></span>Created assertions</h3>
<p>The generator creates assertions for each property and public field. The assertions created are specific to the property/field type. The following list shows which assertions are created for a given type :</p>
<ul>
<li><span class="small-code">boolean</span> :
<ul>
<li><span class="small-code">isX()</span></li>
<li><span class="small-code">isNotX()</span></li>
</ul>
</li>
<li><span class="small-code">int</span>, <span class="small-code">long</span>, <span class="small-code">short</span>, <span class="small-code">byte</span>, <span class="small-code">char</span> :
<ul>
<li><span class="small-code">hasX(value)</span> - values compared with <span class="small-code">==</span></li>
</ul>
</li>
<li><span class="small-code">float</span> and <span class="small-code">double</span> :
<ul>
<li><span class="small-code">hasX(value)</span> - values compared with <span class="small-code">==</span></li>
<li><span class="small-code">hasXCloseTo(value, delta)</span>, diff < delta check</li>
</ul>
</li>
<li><span class="small-code">Iterable<T></span> and <span class="small-code">T[]</span> :
<ul>
<li><span class="small-code">hasX(T... values)</span></li>
<li><span class="small-code">hasOnlyX(T... values)</span></li>
<li><span class="small-code">doesNotHaveX(T... values)</span></li>
<li><span class="small-code">hasNoX()</span></li>
</ul>
Element comparison is based on the <span class="small-code">T::equals</span> method.
</li>
<li><span class="small-code">Object</span> :
<ul>
<li><span class="small-code">hasX(value)</span> - <span class="small-code">equals</span> based comparison</li>
</ul>
</li>
</ul>
<h5>Notes</h5>
<p>The generator introspects properties and public fields (although it can include private fields too with the <a href="assertj-assertions-generator-maven-plugin.html#configuration">generateAssertionsForAllFields</a> option).</p>
<p>The same assertions are generated for primitive types and their wrapper versions (ex: <span class="small-code">double</span> and <span class="small-code">Double</span>), the only difference being the performed check : null safe <span class="small-code">equals</span> comparison for wrappers and <span class="small-code">==</span> for primitives.</p>
<p>You can change what is generated by modifying the <a href="#generated-assertions-templates">templates</a> used to generate assertions, templates are located in the templates directory.</p>
<h4 class="page-header">Example</h4>
<p>Let's enrich our <span class="small-code">Player</span> with different properties/public fields to see what assertions are generated :</p>
<pre><code class="java">public class Player {
// no getter needed to generate assertion for public fields
// private fields getters and setters omitted for brevity
public String name; // Object assertion generated
private int age; // whole number assertion generated
private double height; // real number assertion generated
private boolean retired; // boolean assertion generated
private List<String> teamMates; // Iterable assertion generated
}</code></pre>
<p>Created assertions :</p>
<pre><code class="java">public class PlayerAssert extends AbstractObjectAssert<PlayerAssert, Player> {
// javadoc and implementation omitted for brevity !
public PlayerAssert hasAge(int age) { ... }
public PlayerAssert hasHeight(double height) { ... }
public PlayerAssert hasHeightCloseTo(double height, double offset) { ... }
public PlayerAssert hasTeamMates(String... teamMates) { ... }
public PlayerAssert hasOnlyTeamMates(String... teamMates) { ... }
public PlayerAssert doesNotHaveTeamMates(String... teamMates) { ... }
public PlayerAssert hasNoTeamMates() { ... }
public PlayerAssert isRetired() { ... }
public PlayerAssert isNotRetired() { ... }
public PlayerAssert hasName(String name) { ... }
public PlayerAssert(Player actual) {
super(actual, PlayerAssert.class);
}
public static PlayerAssert assertThat(Player actual) {
return new PlayerAssert(actual);
}
}
</code></pre>
<h3 class="page-header"><span id="generated-entry-points"></span>Generated entry point classes</h3>
<p>The generator will also create entry point classes like <span class="small-code">Assertions</span>, <a href="assertj-core-news.html#assertj-core-1.6.0-bdd-assertions-style"><span class="small-code">BddAssertions</span></a> and <a href="http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#soft-assertions">SoftAssertions/JUnitSoftAssertions</a> to ease accessing each generated <span class="small-code">*Assert</span> class.</p>
<p>Let's say for example that the generator has been applied on the <span class="small-code">Player</span> and <span class="small-code">Game</span> classes. It will have created <span class="small-code">PlayerAssert</span> and <span class="small-code">GameAssert</span> classes and an <span class="small-code">Assertions</span> class looking like this :</p>
<pre><code class="java">// the generator selects the base package of classes it generates assertions for
package org.player;
public class Assertions {
/**
* Creates a new instance of <code>{@link GameAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static GameAssert assertThat(Game actual) {
return new GameAssert(actual);
}
/**
* Creates a new instance of <code>{@link PlayerAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static PlayerAssert assertThat(Player actual) {
return new PlayerAssert(actual);
}
/**
* Creates a new <code>{@link Assertions}</code>.
*/
protected Assertions() {
// empty
}
}</code></pre>
<p>Now you just have to statically import <span class="small-code">org.player.Assertions.assertThat</span> to use custom assertions for <span class="small-code">Player</span> and <span class="small-code">Game</span> in your tests :</p>
<pre><code class="java">import static org.player.Assertions.assertThat;
...
assertThat(mj).hasName("Michael Jordan")
.hasTeam("Chicago Bulls")
.hasTeamMates("Scottie Pippen", "Tony Kukoc");</code></pre>
<h3 class="page-header"><span id="generated-assertions-templates"></span>Assertions templates</h3>
<p>The assertion generator uses templates to create assertions, templates are located in the <a href="#quickstart">templates directory</a>. There is a template for each type and templates for assert class and entry points.</p>
<p>You can (and are encouraged to) modify the templates to generate additional or different assertions, but be sure to generate code that compiles ! :) </p>
<p>The templates contain <a href="#template-variables">variables</a> describing the properties, fields and classes for which assertions are generated. These variables are replaced by the actual values read for each class we generate assertions for.</p>
<h4 class="page-header">Example</h4>
<p>Let's see how it works with the template used to generate <span class="small-code">hasName(String name)</span> assertion for <span class="small-code">Player</span>.</p>
<p>has_assertion_template.txt :</p>
<pre><code class="java">/**
* Verifies that the actual ${class_to_assert}'s ${property} is equal to the given one.
* @param ${property_safe} the ${property} to compare the actual ${class_to_assert}'s ${property} to.
* @return this assertion object.
* @throws AssertionError - if actual ${class_to_assert}'s ${property} is not equal to the given one.${throws_javadoc}
*/
public ${self_type} has${Property}(${propertyType} ${property_safe}) ${throws}{
// check that actual ${class_to_assert} we want to make assertions on is not null.
isNotNull();
// overrides the default error message with a more explicit one
String assertjErrorMessage = "\nExpecting ${property} of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>";
// null safe check
${propertyType} actual${Property} = actual.get${Property}();
if (!Objects.areEqual(actual${Property}, ${property_safe})) {
failWithMessage(assertjErrorMessage, actual, ${property_safe}, actual${Property});
}
// return the current assertion for method chaining
return ${myself};
}</code></pre>
<p>Variables used to generate <span class="small-code">hasName(String name)</span> assertion :</p>
<table>
<tr>
<th>Variable</th>
<th>Usage</th>
<th>Example</th>
</tr>
<tr>
<td>${property}</td>
<td>property/field we generate an assertion for</td>
<td>name</td>
</tr>
<tr>
<td>${propertyType}</td>
<td>The type of the property/field we generate an assertion for</td>
<td>String</td>
</tr>
<tr>
<td>${Property}</td>
<td>property/field we generate an assertion for, with a capital letter</td>
<td>Name</td>
</tr>
<tr>
<td>${property_safe}</td>
<td>${property} if not equal to a reserved java keyword, expected${Property} otherwise</td>
<td>name</td>
</tr>
<tr>
<td>${throws}</td>
<td>the exceptions thrown by the property getter (if any)</td>
<td>-</td>
</tr>
<tr>
<td>${throws_javadoc}</td>
<td>javadoc of the exceptions thrown by the property getter (if any)</td>
<td>-</td>
</tr>
<tr>
<td>${class_to_assert}</td>
<td>Class we are generating assertion for</td>
<td>Player</td>
</tr>
<tr>
<td>${self_type}</td>
<td>the assertion class name</td>
<td>PlayerAssert</td>
</tr>
<tr>
<td>${myself}</td>
<td>represents the assertion class instance used to perform assertions</td>
<td>this</td>
</tr>
</table>
<p><br/>And finally here's the generated assertion :</p>
<pre><code class="java">/**
* Verifies that the actual Player's name is equal to the given one.
* @param name the name to compare the actual Player's name to.
* @return this assertion object.
* @throws AssertionError - if the actual Player's name is not equal to the given one.
*/
public PlayerAssert hasName(String name) {
// check that actual Player we want to make assertions on is not null.
isNotNull();
// overrides the default error message with a more explicit one
String assertjErrorMessage = "\nExpecting name of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>";
// null safe check
String actualName = actual.getName();
if (!Objects.areEqual(actualName, name)) {
failWithMessage(assertjErrorMessage, actual, name, actualName);
}
// return the current assertion for method chaining
return this;
}</code></pre>
<h4 class="page-header"><span id="templates-list" class="adjustAnchor"></span>Templates list</h4>
<p>Here's the list of template files and what they are used for.</p>
<h5>Assertions method templates</h5>
<table>
<tr>
<th>Assertion method template file</th>
<th>Usage</th>
<th>Example</th>
</tr>
<tr>
<td>has_assertion_template.txt</td>
<td>assertion for <span class="small-code">Object</span> (used unless a more specific template is found)</td>
<td><span class="small-code">hasName("Joe")</span></td>
</tr>
<tr>
<td>is_assertion_template.txt</td>
<td>assertion for <span class="small-code">boolean</span></td>
<td><span class="small-code">isRookie()</span></td>
</tr>
<tr>
<td>is_wrapper_assertion_template.txt</td>
<td>assertion for <span class="small-code">Boolean</span></td>
<td><span class="small-code">isRookie()</span></td>
</tr>
<tr>
<td>has_assertion_template_for_char.txt</td>
<td>assertion for <span class="small-code">char</span> primitive type</td>
<td><span class="small-code">hasGender('F')</span></td>
</tr>
<tr>
<td>has_assertion_template_for_character.txt</td>
<td>assertion for <span class="small-code">Character</span></td>
<td><span class="small-code">hasGender('F')</span></td>
</tr>
<tr>
<td>has_assertion_template_for_whole_number.txt</td>
<td>assertion for <span class="small-code">int</span>, <span class="small-code">long</span>, <span class="small-code">short</span> and <span class="small-code">byte</span></td>
<td><span class="small-code">hasAge(23)</span></td>
</tr>
<tr>
<td>has_assertion_template_for_whole_number_wrapper.txt</td>
<td>assertion for <span class="small-code">Integer</span>, <span class="small-code">Long</span>, <span class="small-code">Short</span> and <span class="small-code">Byte</span></td>
<td><span class="small-code">hasAge(23)</span></td>
</tr>
<tr>
<td>has_assertion_template_for_real_number.txt</td>
<td>assertion for <span class="small-code">float</span> and <span class="small-code">double</span></td>
<td><span class="small-code">hasHeight(1.87)</span></td>
</tr>
<tr>
<td>has_assertion_template_for_real_number_wrapper.txt</td>
<td>assertion for <span class="small-code">Float</span> and <span class="small-code">Double</span></td>
<td><span class="small-code">hasHeight(1.87)</span></td>
</tr>
<tr>
<td>has_elements_assertion_template_for_array.txt</td>
<td>assertion for array.</td>
<td><span class="small-code">hasTeamMates("mj")</span></td>
</tr>
<tr>
<td>has_elements_assertion_template_for_iterable.txt</td>
<td>assertion for <span class="small-code">Iterable</span>.</td>
<td><span class="small-code">hasTeamMates("mj")</span></td>
</tr>
</table>
<h5>Assertions class templates</h5>
<table width="100%">
<tr>
<th>Assertion class template file</th>
<th>Usage</th>
</tr>
<tr>
<td>custom_assertion_class_template.txt</td>
<td>assert class skeleton</td>
</tr>
<tr>
<td>custom_abstract_assertion_class_template.txt</td>
<td>base assertion class in in hierarchical assertion</td>
</tr>
<tr>
<td>custom_hierarchical_assertion_class_template.txt</td>
<td>concrete assertion class in in hierarchical assertion</td>
</tr>
</table>
<h5>Assertions entry point templates</h5>
<table>
<tr>
<th>Entry point class/methods template file</th>
<th>Usage</th>
</tr>
<tr>
<td>standard_assertions_entry_point_class_template.txt</td>
<td><span class="small-code">Assertions</span> class skeleton</td>
</tr>
<tr>
<td>standard_assertion_entry_point_method_template.txt</td>
<td>template for <span class="small-code">assertThat</span> method in entry point class like <span class="small-code">Assertions</span></td>
</tr>
<tr>
<td>bdd_assertions_entry_point_class_template.txt</td>
<td><span class="small-code">BddAssertions</span> class skeleton</td>
</tr>
<tr>
<td>bdd_assertion_entry_point_method_template.txt</td>
<td>template for <span class="small-code">then</span> method in <span class="small-code">BddAssertions</span> entry point class</td>
</tr>
<tr>
<td>soft_assertions_entry_point_class_template.txt</td>
<td><span class="small-code">SoftAssertions</span> class skeleton</td>
</tr>
<tr>
<td>soft_assertion_entry_point_method_template.txt</td>
<td>template for "soft" <span class="small-code">assertThat</span> method in <span class="small-code">SoftAssertions</span> entry point class</td>
</tr>
<tr>
<td>junit_soft_assertions_entry_point_class_template.txt</td>
<td><span class="small-code">JUnitSoftAssertions</span> class skeleton</td>
</tr>
</table>
<h4 class="page-header"><span id="template-variables" class="adjustAnchor"></span>Variables used in templates</h4>
<p>Variables used in the assertions templates:</p>
<table>
<tr>
<th colspan=3>Common variables (assertion level)</th>
</tr>
<tr>
<td>${property}</td>
<td>property/field we generate an assertion for</td>
</tr>
<tr>
<td>${propertyType}</td>
<td>The type of the property/field we generate an assertion for</td>
</tr>
<tr>
<td>${Property}</td>
<td>property/field we generate an assertion for, with a capital letter</td>
</tr>
<tr>
<td>${property_safe}</td>
<td>${property} if not equal to a reserved java keyword, expected${Property} otherwise</td>
</tr>
<tr>
<td>${throws}</td>
<td>the exceptions thrown by the property getter (if any)</td>
</tr>
<tr>
<td>${throws_javadoc}</td>
<td>javadoc of the exceptions thrown by the property getter (if any)</td>
</tr>
<tr>
<td>${class_to_assert}</td>
<td>Class we are generating assertion for</td>
</tr>
<tr>
<td>${self_type}</td>
<td>the assertion class name</td>
</tr>
<tr>
<td>${myself}</td>
<td>represents the assertion class instance used to perform assertions</td>
</tr>
<tr>
<th colspan=2>Variables specific to boolean/predicate assertions</th>
</tr>
<tr>
<td>${predicate}</td>
<td>predicate assertion method name. ex: <span class="small-code">isRookie()</span></td>
</tr>
<tr>
<td>${neg_predicate}</td>
<td>negative predicate assertion method name. ex: <span class="small-code">isNotRookie()</span></td>
</tr>
<tr>
<td>${predicate_for_error_message_part1}</td>
<td>used to build the first part of a predicate error message.</td>
</tr>
<tr>
<td>${predicate_for_error_message_part2}</td>
<td>used to build the second part of a predicate error message.</td>
</tr>
<tr>
<td>${negative_predicate_for_error_message_part1}</td>
<td>used to build the first part of a negative predicate error message.</td>
</tr>
<tr>
<td>${negative_predicate_for_error_message_part2}</td>
<td>used to build the second part of a negative predicate error message.</td>
</tr>
<tr>
<td>${predicate_for_javadoc}</td>
<td>predicate assertion method javadoc</span></td>
</tr>
<tr>
<td>${negative_predicate_for_javadoc}</td>
<td>negative predicate assertion method javadoc</span></td>
</tr>
<tr>
<th colspan=2>Variables specific to array assertions</th>
</tr>
<tr>
<td>${elementType}</td>
<td>the element type of the array property. ex: <span class="small-code">String</span> for a <span class="small-code">String[]</span> property</td>
</tr>
<tr>
<th colspan=2>Variables specific to assertion class</th>
</tr>
<tr>
<td>${custom_assertion_class}</td>
<td>the assertion class name. ex: <span class="small-code">PlayerAssert</span></td>
</tr>
<tr>
<td>${super_assertion_class}</td>
<td>used for the super class in hierarchical assertions.</td>
</tr>
<tr>
<td>${imports}</td>
<td>imports off the current generated assertion class.</td>
</tr>
<tr>
<td>${package}</td>
<td>assertion class package (same as the class we generate assertion for).</td>
</tr>
<tr>
<th colspan=2>Variables specific to assertion entry points classes</th>
</tr>
<tr>
<td>${all_assertions_entry_points}</td>
<td>all the generated entry points. ex: <span class="small-code">assertThat</span> for <span class="small-code">Assertions</span> or <span class="small-code">then</span> for <span class="small-code">BddAssertions</span></td>
</tr>
</table>
<h3 class="page-header"><span id="limitations"></span>Limitations</h3>
<h4 class="page-header">Generated code can be incorrect</h4>
<p>There are a few cases where the generator is not able to generate code that is 100% correct, notably for some generic types that expose generic fields or properties like <a href="http://hapifhir.io/apidocs-dstu3/org/hl7/fhir/dstu3/model/PrimitiveType.html">FHIR PrimitiveType</a>.</p>
<p>In that case, you can exclude the offending classes from having assertions being generated or fix the generated code manually (and put it in source control).</p>
<p>As a reminder, the <a href="#philosophy">philosophy</a> of the generator is to lower the barrier to have custom assertions, if 99% of the generated code is correct and the remaining is easy to fix, then we consider the job done because you were able to get a lot custom assertions with limited effort..</p>
<h4 class="page-header">No support for generics</h4>
<p>The generator does not handle generics in types it creates assertions for, we have tried (hard!) adding generics support, it turns out to be a really complex task especially handling correctly generics bounds. We came to a 90% support but the remaining 10% were difficult to solve and it made the code brittle overall. The work in progress is in <a href="https://github.com/joel-costigliola/assertj-assertions-generator/tree/generics-support">generics-support</a> branch for anyone who wants to give it a go.</p>
<h3 class="page-header"><span id="quickstart"></span>Quickstart for NON Maven users</h3>
<p>This quickstart guide is for non maven users, if you are a maven user, you should use the <a href="assertj-assertions-generator-maven-plugin.html">assertions generator maven plugin</a>.</p>
<p>Gradle users can use the <a href="https://github.com/assertj/assertj-generator-gradle-plugin">gradle plugin</a> or look at this <a href="https://github.com/joel-costigliola/assertj-assertions-generator/blob/master/src/main/scripts/build.gradle">gradle build script</a> to run the generator from Gradle.</p>
<h4 class="page-header">1 - Get AssertJ assertions generator</h4>
<p>Get the AssertJ Assertions Generator <a href="http://search.maven.org/#search|ga|1|a%3A%22assertj-assertions-generator%22">archive</a> (either for unix or windows) and extract it, its structure is :</p>
<pre><code class="bash">assertion-generator/
|-- lib/
|-- templates/
|-- generate-assertions script (.bat or .sh)</code></pre>
<h4 class="page-header">2 - Generate assertions</h4>
<p>First, put in <span class="small-code">lib/</span> the jars containing the classes you want to generate assertions for,
then run the script with either the classes or the packages containing the classes you want to generate assertions for.</p>
<p>Example : generating assertions for <span class="small-code">TolkienCharacter</span> class :</p>
<pre><code class="bash">generate-assertions.sh org.assertj.examples.data.TolkienCharacter</code></pre>
<p>Example : generating assertions for all classes in <span class="small-code">org.assertj.examples.data</span> :</p>
<pre><code class="bash">generate-assertions.sh org.assertj.examples.data</code></pre>
<p>Your assertions classes have been generated ! You can find them in the current directory.</p>
<h4 class="page-header">Tip - Change generated assertions</h4>
<p>You can change what is generated by modifying the templates used for assertions creation (have a look at templates directory)</p>
<h3 class="page-header"><span id="news"></span><span id="2.1.0"></span>Assertions generator 2.1.0 release</h3>
<p>Release date : 2017-10-01</p>
<p>Thanks to <span class="contributor">Sami Paavilainen</span>, <span class="contributor">Pascal Schumacher</span>, <span class="contributor">Gaël Lhez</span>, <span class="contributor">James Strachan</span>, <span class="contributor">Brian Norman</span>, <span class="contributor">Kevin Brightwell</span> and <span class="contributor">Christian Wiejack</span> for their contributions to this release.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Allow to generate assertions for all fields whatever their visibility is.</li>
<li>Generate BDD Soft assertions.</li>
<li>Generate JUnit BDD Soft assertions.</li>
<li>Generate <span class="small-code">AutoCloseableSoftAssertions</span>.</li>
<li>For iterable properties, generate <span class="small-code">hasXXXs</span>, <span class="small-code">hasOnlyXXXs</span> and <span class="small-code">doesNotHaveXXXs</span> taking a collection argument.
<li>Allow to set in which package assertions are generated.</li>
<li>Allow to generate assertions for annotated methods or classes with <span class="small-code">GenerateAssertion</span>.</li>
<li>Add <span class="small-code">@javax.annotation.Generated</span> to generated classes.</li>
<li>Generated assertions now extend <span class="small-code">AbstractObjectAssert</span> providing common object assertions.</li>
<li>Make generated assertions extend <span class="small-code">AbstractComparableAssert</span> when possible providing comparable assertions.</li>
<li>Generate assertions methods with <span class="small-code">@CheckReturnValue</span> to detect incorrect usage.</li>
<li>Play more friendly with post instrumentalization tool like jacoco.</li>
<li>Fix : avoid potential clash with offset parameter by renaming it to assertjOffset.</li>
<li>Fix : generate correct file name when assertions entry point template class name is changed.</li>
<li>Fix : could not find predicate methods that collide.</li>
<li>Fix : some javadoc tags were not correct.</li>
<li>Fix : parent class import was missing when it was not in same package as its child.</li>
<li>Fix : explicitly exclude <span class="small-code">package-info</span> classes from being processed for assertion generation.</li>
<li>Fix : consistently avoid generating assertions for non public classes.</li>
</ul>
<div class="release-section-category">Template notable changes:</div>
<ul>
<li>custom_abstract_assertion_class_template.txt: add <span class="small-code">${imports}</span>.</li>
<li>All has_assertion_xxx templates: <span class="small-code">get${Property}()</span> changed to <span class="small-code">actual.${getter}()</span></li>
<li>All has_elements_xxx templates: <span class="small-code">get${Property}()</span> changed to <span class="small-code">actual.${getter}()</span></li>
</ul>
<h3 class="page-header"><span id="2.0.0"></span>Assertions generator 2.0.0 release</h3>
<p>Release date : 2015-08-01</p>
<p>Thanks to <span class="contributor">Alexander Bischof</span> for his contributions to this release.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Uses java 7.</li>
<li>Define different assertion templates for whole number, real numbers and char properties.</li>
<li>Document how assertion templates are used to generate assertions.</li>
<li>Generate both <span class="small-code">hasValue(v)</span> and <span class="small-code">hasValueCloseTo(v, delta)</span> for real numbers properties.</li>
<li>Fix : Predicate assertion was badly generated when boolean property contained 'is'.</li>
<li>Fix : Predicate assertion is now generated for <span class="small-code">Boolean</span> property not starting by 'is'.</li>
</ul>
<h3 class="page-header"></span><span id="1.6.0"></span>Assertions generator 1.6.0 release</h3>
<p>Release date : 2015-04-11</p>
<p>Huge thanks to <span class="contributor">Jeremy Krieg</span> for his contributions to this release.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Generate <span class="small-code">hasOnly</span> and <span class="small-code">doesNotHave</span> assertions for array/iterable properties.</li>
<li>Better error messages.</li>
<li>Sanitise property names when they clash with Java keywords. <span class="contributor">(Fr Jeremy Krieg)</span></li>
<li>Skip generation of field assertion if it would cause a name clash with a property assertion. <span class="contributor">(Fr Jeremy Krieg)</span></li>
<li>Handle other boolean predicate forms than <span class="small-code">is</span> like <span class="small-code">was</span>, <span class="small-code">can</span>, <span class="small-code">will</span>, <span class="small-code">shouldBe</span>. <span class="contributor">(Fr Jeremy Krieg)</span></li>
<li>Generated iterable/array assertions did not to honour the description set with <span class="small-code">as()</span> method when given iterable/array was null. </li>
</ul>
<h3 class="page-header"></span><span id="1.5.0"></span>Assertions generator 1.5.0 release</h3>
<p>Release date : 2014-11-02</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Generate <span class="small-code">JUnitSoftAssertions</span>.</li>
<li>Fix potential import clash by using fully qualified class names (except for AssertJ classes).</li>
<li>Fix class cast exception when an overriden getter returns a generic type.</li>
</ul>
<h3 class="page-header"></span><span id="1.4.0"></span>Assertions generator 1.4.0 release</h3>
<p>Release date : 2014-08-31</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Fixes bad generated code in hasNoXXX assertion for iterable property.</li>
<li>Fixes bad generated code in contains assertion for iterable property with upper bound wildcard (<span class="small-code">? extends</span>).</li>
<li>Fix to generate assertion for classes having '$' character in class name.</li>
<li>Fix fenerated assertion for public boolean field.</li>
<li>Use fully qualified name class instead of import in Assertions entry point classes.</li>
</ul>
<h3 class="page-header"><span id="1.3.1"></span>Assertions generator 1.3.1 release</h3>
<p>Release date : 2014-08-17</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Correctly resolve generic return types.</li>
<li>Use Guava ClassPath instead of org.reflections.</li>
</ul>
<h3 class="page-header"></span><span id="1.3.0"></span>Assertions generator 1.3.0 release</h3>
<p>Release date : 2014-08-03</p>
<p>Big thanks to <span class="contributor">Fr Jeremy Krieg</span> for his work on this release.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Generate assertions using offset for double and float types.</li>
<li>Allow to generate assertion classes that don't hide subclasses assertions after using an assertion from a super class. <span class="contributor">(Fr Jeremy Krieg)</span></li>
<li>Generate assertions for public fields.</li>
<li>Prepend enclosing class name to inner class assertion name to avoid clash when several inner classes have the same name.</li>
<li>Don't generate classes with unused import. <span class="contributor">(Fr Jeremy Krieg)</span></li>
<li>Generated javadoc improvements. <span class="contributor">(Fr Jeremy Krieg)</span></li>
<li>Allow to specify where to generate Assertions entry point classes. <span class="contributor">(floppywaste)</span></li>
<li>Generate a BDD style entry point class <a href="assertj-core-news.html#assertj-core-1.6.0-bdd-assertions-style"><span class="small-code">BddAssertions</span></a> replacing <span class="small-code">assertThat</span> methods by <span class="small-code">then</span>.</li>
<li>Generate a <a href="http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#soft-assertions">SoftAssertions</a> entry point class to collect all assertion errors instead of stopping at the first one.</li>
</ul>
<h3 class="page-header"><span id="1.2.0"></span>Assertions generator 1.2.0 release</h3>
<p>Release date : 2013-12-08</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>To ease using generated assertions, generate an entry point class <span class="small-code">Assertions</span> providing <span class="small-code">assertThat</span> methods giving access to each generated <span class="small-code">*Assert</span> classes.</li>
<li>Don't generate <span class="small-code">hasDeclaringClass</span> assertion for <span class="small-code">Enum</span>.</li>
<li>In generated method assertions, rename <span class="small-code">errorMessage</span> local variable to <span class="small-code">assertjErrorMessage</span> to avoid collision when object under test has an <span class="small-code">errorMessage</span> property. <span class="contributor">(William Delanoue)</span></li>
</ul>
<h3 class="page-header"><span id="assertions-generator-1.1.0"></span>Assertions generator 1.1.0 release</h3>
<p>Release date : 2013-09-15</p>
<p>Big thanks to <span class="contributor">Fabien Duminy</span>, he has done most of the work of this release !</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Generated assertions use null safe equals for non primitive types.</li>
<li>Add exceptions to assertion method signature if underlying getter of class to assert throws some</span>. <span class="contributor">(Fabien Duminy)</span></li>
<li>Generate assertions for nested/inner classes. <span class="contributor">(Fabien Duminy)</span></li>
<li>Fix error message displaying unrelevant property value when getter of class to assert doesn't return the same value on successive calls. <span class="contributor">(Fabien Duminy)</span></li>
</ul>
<h3 class="page-header"><span id="assertions-generator-1.0.0"></span>Assertions generator 1.0.0 release</h3>
<p>Release date : 2013-03-26</p>
<p>It's the first release after Fest fork, generated assertions are cleaner.</p>
<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 assertions generator is hosted on github : <a href="https://github.com/joel-costigliola/assertj-assertions-generator">https://github.com/joel-costigliola/assertj-assertions-generator</a>.</p>
<p>Please report bugs or missing features in <a href="https://github.com/joel-costigliola/assertj-assertions-generator/issues?labels=&milestone=&page=1&state=open">AssertJ assertions generator 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-assertions-generator/blob/master/CONTRIBUTING.md#contributing">contributor's guidelines</a>.</p>
<p>Special thanks to AssertJ assertions generator contributors:</p>
<ul>
<li>Fabien Duminy</li>
<li>William Delanoue</li>
<li>floppywaste</li>
<li>Fr Jeremy Krieg</li>
</ul>
</div>
</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>