forked from joel-costigliola/assertj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertj-joda-time.html
348 lines (268 loc) · 19.5 KB
/
assertj-joda-time.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
<!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-joda-time.html">Overview</a></li>
<li><a href="assertj-joda-time.html#quickstart">Quick start</a></li>
<li><a href="assertj-joda-time.html#tips">Tips and tricks</a></li>
<li><a href="assertj-joda-time.html#latest-release">Releases</a></li>
<li><a href="http://joel-costigliola.github.io/assertj/jodatime/api/index.html" target="_blank">Javadoc</a></li>
<li><a href="assertj-joda-time.html#migrating-from-fest">Migrating from Fest</a></li>
<li><a href="assertj-joda-time.html#mailing-list">Mailing list</a></li>
<li><a href="assertj-joda-time.html#code">Code & issues <i class="fa fa-github"></i></a></li>
<li><a href="assertj-joda-time.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 for Joda-Time</h1>
<p>AssertJ assertions for <a href="http://www.joda.org/joda-time/">Joda-Time</a> provides assertions for Joda-Time types like <span class="small-code">DateTime</span> and <span class="small-code">LocalDateTime</span>.<br>
If you need additional assertions, just create a ticket in the <a href="https://github.com/joel-costigliola/assertj-joda-time/issues?labels=&milestone=&page=1&state=open">AssertJ Joda-Time issue tracker</a>.</p>
<p>AssertJ Joda-Time is hosted on github : <a href="https://github.com/joel-costigliola/assertj-joda-time">https://github.com/joel-costigliola/assertj-joda-time</a>.</p>
<h3 class="page-header"><span id="quickstart"></span>Joda-Time assertions quickstart guide</h3>
<p>To quickly start using Joda-Time assertions, follow the steps below.</p>
<h4 class="page-header">1 - Add assertj-joda-time dependency to your project pom.xml</h4>
<pre><code class="xml"><dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-joda-time</artifactId>
<!-- use 1.1.0 for Java 7 projects -->
<version>2.2.0</version>
<scope>test</scope>
</dependency></code></pre>
<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-joda-time|2.2.0|bundle">assertj-joda-time dependency declaration</a> for your tool.</p>
<h4 class="page-header">2 - Statically import <span class="small-code">org.assertj.jodatime.api.Assertions.assertThat</span> ...</h4>
<p>... and use your preferred IDEs code completion after <span class="small-code">assertThat.</span></p>
<p>Examples :</p>
<pre><code class="java">import static org.assertj.jodatime.api.Assertions.assertThat;
...
assertThat(dateTime).isBefore(firstDateTime);
assertThat(dateTime).isAfterOrEqualTo(secondDateTime);
// you can use Strings in comparisons to avoid a conversion (we do that for you)
assertThat(new DateTime("2000-01-01")).isEqualTo("2000-01-01");
// compare DateTimes ignoring seconds and milliseconds in the comparison
dateTime1 = new DateTime(2000, 1, 1, 23, 50, 0, 0, UTC);
dateTime2 = new DateTime(2000, 1, 1, 23, 50, 10, 456, UTC);
// assertion succeeds
assertThat(dateTime1).isEqualToIgnoringSeconds(dateTime2);</code></pre>
<p>For <span class="small-code">DateTime</span> assertions, comparison is performed in the <span class="small-code">DateTimeZone</span> of <span class="small-code">DateTime</span> to test, consequently the following assertion passes :</p>
<pre><code class="java">DateTime utcTime = new DateTime(2013, 6, 10, 0, 0, DateTimeZone.UTC);
DateTime cestTime = new DateTime(2013, 6, 10, 2, 0, DateTimeZone.forID("Europe/Berlin"));
assertThat(utcTime).as("in UTC time").isEqualTo(cestTime);</code></pre>
<p>You can compare <span class="small-code">DateTime</span> to another <span class="small-code">DateTime</span>, or <span class="small-code">LocalDateTime</span> to <span class="small-code">LocalDateTime</span>, but not <span class="small-code">DateTime</span> to <span class="small-code">LocalDateTime</span>, it doesn't make sense as one is timezone dependent and the other is not.</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/JodaTimeAssertionsExamples.java">JodaTimeAssertionsExamples.java</a> class of the <a href="https://github.com/joel-costigliola/assertj-examples/">assertj-examples</a> project.</p>
<h3 class="page-header"><span id="tips"/></span>Tips & tricks</h3>
<h5>Using date string representation</h5>
<p>To be even easier to use, one can specify <span class="small-code">DateTime</span> or <span class="small-code">LocalDateTime</span> with their String representation to avoid manual String conversion, like in the example below :</p>
<p>Example :</p>
<pre><code class="java">// instead of writing ...
assertThat(dateTime).isBefore(new DateTime("2004-12-13T21:39:45.618-08:00"));
// ... you can simply write (if you prefer)
assertThat(dateTime).isBefore("2004-12-13T21:39:45.618-08:00");</code></pre>
<h5>Comparing with a precision level</h5>
<p>It is possible to compare <span class="small-code">DateTime</span> or <span class="small-code">LocalDateTime</span> ignoring time fields below a given a precision.
This is handy if you don't want, for example, to consider minutes in the comparison (which also implies to ignore seconds and milliseconds too).</p>
<p>Example :</p>
<pre><code class="java">import static org.assertj.jodatime.api.Assertions.assertThat;
// compare DateTime or LocalDateTime with a precision level, ignoring time fields
dateTime1 = new DateTime(2000, 1, 1, 23, 50, 0, 0, UTC);
dateTime2 = new DateTime(2000, 1, 1, 23, 50, 10, 456, UTC);
// ignore seconds and milliseconds in the comparison
assertThat(dateTime1).isEqualToIgnoringSeconds(dateTime2);</code></pre>
<h3 class="page-header"><span id="latest-release"/></span><span id="assertj-joda-time-2.2.0"/></span>Joda-Time assertions 2.2.0 release</h3>
<p>Release date : 2018-06-15</p>
<p>Thanks to <span class="contributor">Eugene Strepetov</span> for his contributions.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Add <span class="small-code">LocalDate</span> assertions providing the following ones (<span class="contributor">Eugene Strepetov</span>): </li>
<ul>
<li><span class="small-code">hasYear</span></li>
<li><span class="small-code">hasMonthOfYear</span></li>
<li><span class="small-code">hasDayOfMonth</span></li>
<li><span class="small-code">isBefore</span></li>
<li><span class="small-code">isBeforeOrEqualTo</span></li>
<li><span class="small-code">isEqualTo</span></li>
<li><span class="small-code">isAfter</span></li>
<li><span class="small-code">isAfterOrEqualTo</span></li>
<li><span class="small-code">isIn</span></li>
<li><span class="small-code">isNotEqualTo</span></li>
<li><span class="small-code">isNotIn</span></li>
</ul>
<li>Javadoc improvements.</li>
</ul>
<h4 class="page-header"><i class="fa fa-arrow-circle-right"></i> <span class="small-code">LocalDate</span> assertions examples</h4>
<pre><code class='java'>LocalDate localDate = new LocalDate(2000, 1, 1);
assertThat(localDate).hasYear(2000)
.hasMonthOfYear(1)
.hasDayOfMonth(1)
.isBefore(new LocalDate(2000, 1, 2))
.isBefore("2000-01-02")
.isBeforeOrEqualTo(new LocalDate(2000, 1, 1))
.isBeforeOrEqualTo("2000-01-01")
.isBeforeOrEqualTo(new LocalDate(2000, 1, 2))
.isBeforeOrEqualTo("2000-01-02")
.isEqualTo(new LocalDate(2000, 1, 1))
.isEqualTo("2000-01-01")
.isAfterOrEqualTo(new LocalDate(2000, 1, 1))
.isAfterOrEqualTo("2000-01-01")
.isAfterOrEqualTo(new LocalDate(1999, 12, 31))
.isAfterOrEqualTo("1999-12-31")
.isAfter(new LocalDate(1999, 12, 31))
.isAfter("1999-12-31")
.isNotEqualTo("2000-01-15")
.isNotEqualTo(new LocalDate(2000, 1, 15))
.isIn(new LocalDate(1999, 12, 31), new LocalDate(2000, 1, 1))
.isIn("1999-12-31", "2000-01-01")
.isNotIn(new LocalDate(1999, 12, 31), new LocalDate(2000, 1, 2))
.isNotIn("1999-12-31", "2000-01-02");
</code></pre>
<h3 class="page-header"><span id="latest-release"/></span><span id="assertj-joda-time-2.1.0"/></span>Joda-Time assertions 2.1.0 release</h3>
<p>Release date : 2018-05-27</p>
<p>Thanks to <span class="contributor">John Killmer</span> for his contributions.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>Add the following <span class="small-code">DateTime</span> and <span class="small-code">LocalDateTime</span> assertions (<span class="contributor">John Killmer</span>): </li>
<ul>
<li><span class="small-code">hasYear</span></li>
<li><span class="small-code">hasMonthOfYear</span></li>
<li><span class="small-code">hasDayOfMonth</span></li>
<li><span class="small-code">hasHourOfDay</span></li>
<li><span class="small-code">hasMinuteOfHour</span></li>
<li><span class="small-code">hasSecondOfMinute</span></li>
<li><span class="small-code">hasMillisOfSecond</span></li>
</ul>
<li>Javadoc improvements.</li>
</ul>
<h4 class="page-header"><i class="fa fa-arrow-circle-right"></i> New assertions examples</h4>
<pre><code class='java'>DateTime dateTime = new DateTime(1999, 12, 31, 23, 59, 59, 999, DateTimeZone.UTC);
assertThat(dateTime).hasYear(1999)
.hasMonthOfYear(12)
.hasDayOfMonth(31)
.hasHourOfDay(23)
.hasMinuteOfHour(59)
.hasSecondOfMinute(59)
.hasMillisOfSecond(999);</code></pre>
<h3 class="page-header"><span id="assertj-joda-time-2.0.0"/></span>Joda-Time assertions 2.0.0 release</h3>
<p>Release date : 2016-04-10</p>
<p>AssertJ Joda Time 2.0.0 requires Java 8 as it relies on AssertJ Core 3.x, use 1.1.0 version if you are still using Java 7.</p>
<p>Thanks to <span class="contributor">Pascal Schumacher</span> and <span class="contributor">David Simmons</span> for their contributions.</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li><b>BREAKING CHANGE</b> : requires Java 8.</li>
<li><span class="small-code">DateTime</span> assertion : better handling of null in assertions. (<span class="contributor">David Simmons</span>)</li>
<li>To be OS agnostic, replace <span class="small-code">\n</span> with <span class="small-code">%n</span> in error messages. (<span class="contributor">Pascal Schumacher</span>)</li>
<li>Better spacing in Javadoc code examples. (<span class="contributor">Pascal Schumacher</span>)</li>
</ul>
<h3 class="page-header">Joda-Time assertions 1.1.0 release</h3>
<p>Release date : 2013-07-14</p>
<div class="release-section-category">Release notes:</div>
<ul>
<li>In <span class="small-code">DateTime</span> assertions, comparison is performed in the <span class="small-code">DateTimeZone</span> of the <span class="small-code">DateTime</span> to check.</li>
<li>assertj-joda-time does not impose joda-time 2.2 version anymore, it is compatible with any 2.0+ version.</li>
<li>Multiline error message improvements to make it easier to see what was wrong.</li>
<li>Javadoc improvements : fix inconsistencies and add more code examples.</li>
</ul>
<h3 class="page-header">Joda-Time assertions 1.0.0 release</h3>
<p>2013-03-26 : 1.0.0 release</p>
<p>First release after Fest fork.</p>
<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/jodatime/api/index.html">AssertJ Joda-Time javadoc</a>.</p>
<h3 class="page-header"><span id="migrating-from-fest"/></span>Migrating from Fest Joda-Time assertions</h3>
<p>Migrating from Fest Joda-Time to AssertJ Joda-Time Assertions is easy, you only have to change your static import, just replace :
</p>
<pre><code class="java">import static org.fest.assertions.api.JODA_TIME</code></pre>
<p>by :</p>
<pre><code class="java">import static org.assertj.joda-time.api.Assertions</code></pre>
<p>For linux users, a quick way to migrate is to use the following command:</p>
<pre><code class="bash">find . -name "*.java" -exec sed -i "s/import static org.fest.assertions.api.JODA_TIME/import static org.assertj.jodatime.api.Assertions/g;s/import org.fest.assertions.api.JODA_TIME;/import org.assertj.jodatime.api.Assertions;/g;s/JODA_TIME./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/import static org.fest.assertions.api.JODA_TIME/import static org.assertj.jodatime.api.Assertions/g;s/import org.fest.assertions.api.JODA_TIME;/import org.assertj.jodatime.api.Assertions;/g;s/JODA_TIME./Assertions./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 Joda-Time <strong>before</strong> <a href="assertj-core-migrating-from-fest.html">those for FestAssert Core</a>!</p>
<h3 class="page-header"><span id="core-and-joda-time-assertions"/></span>Using both AssertJ <a href="https://github.com/joel-costigliola/assertj-core">Core assertions</a> and Joda-Time assertions</h3>
<p>You will have to make two static import : 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.jodatime.api.Assertions.assertThat</span> for Joda-Time assertions.</p>
<pre><code class="java">import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.jodatime.api.Assertions.assertThat;
...
// assertThat comes from org.assertj.jodatime.api.Assertions.assertThat static import
assertThat(new DateTime("2000-01-01")).isAfter(new DateTime("1999-12-31"));
// 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 the
<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 Joda-Time is hosted on github : <a href="https://github.com/joel-costigliola/assertj-joda-time">https://github.com/joel-costigliola/assertj-joda-time</a>.</p>
<p>Please report bugs or missing features in the <a href="https://github.com/joel-costigliola/assertj-joda-time/issues?labels=&milestone=&page=1&state=open">AssertJ Joda-Time 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-joda-time/blob/master/CONTRIBUTING.md#contributing">contributor's guidelines</a>.</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>