From 68741eaa2aea90a898345e018bee50f3b1b829bf Mon Sep 17 00:00:00 2001 From: Joel Costigliola Date: Sun, 21 May 2017 15:33:14 +1200 Subject: [PATCH 1/6] Add examples for assertj core 2.8.0 --- .../org/assertj/examples/data/ArtWork.java | 26 +++++++++++ .../assertj/examples/data/movie/Movie.java | 4 +- .../org/assertj/Assertions.java | 10 +++++ .../org/assertj/BddAssertions.java | 10 +++++ .../org/assertj/JUnitSoftAssertions.java | 10 +++++ .../org/assertj/SoftAssertions.java | 10 +++++ .../examples/data/AbstractArtWorkAssert.java | 44 +++++++++++++++++++ .../assertj/examples/data/ArtWorkAssert.java | 27 ++++++++++++ .../data/movie/AbstractMovieAssert.java | 4 +- .../org/assertj/examples/rpg/ItemAssert.java | 6 +-- .../org/assertj/examples/rpg/WandAssert.java | 6 +-- .../GeneratedStandardAssertionsExamples.java | 4 +- .../examples/IterableAssertionsExamples.java | 4 ++ .../examples/NumberAssertionsExamples.java | 15 +++++++ .../examples/StringAssertionsExamples.java | 21 +++++++++ 15 files changed, 189 insertions(+), 12 deletions(-) create mode 100644 assertions-examples/src/main/java/org/assertj/examples/data/ArtWork.java create mode 100644 assertions-examples/src/test/generated-assertions/org/assertj/examples/data/AbstractArtWorkAssert.java create mode 100644 assertions-examples/src/test/generated-assertions/org/assertj/examples/data/ArtWorkAssert.java diff --git a/assertions-examples/src/main/java/org/assertj/examples/data/ArtWork.java b/assertions-examples/src/main/java/org/assertj/examples/data/ArtWork.java new file mode 100644 index 00000000..2774b0b9 --- /dev/null +++ b/assertions-examples/src/main/java/org/assertj/examples/data/ArtWork.java @@ -0,0 +1,26 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2016 the original author or authors. + */ +package org.assertj.examples.data; + +public class ArtWork { + + private String creator; + + public String getCreator() { + return creator; + } + + public void setCreator(String creator) { + this.creator = creator; + } +} diff --git a/assertions-examples/src/main/java/org/assertj/examples/data/movie/Movie.java b/assertions-examples/src/main/java/org/assertj/examples/data/movie/Movie.java index 57261ff6..b62d897a 100644 --- a/assertions-examples/src/main/java/org/assertj/examples/data/movie/Movie.java +++ b/assertions-examples/src/main/java/org/assertj/examples/data/movie/Movie.java @@ -14,7 +14,9 @@ import java.util.Date; -public class Movie { +import org.assertj.examples.data.ArtWork; + +public class Movie extends ArtWork { private String title; private Date releaseDate; diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/Assertions.java b/assertions-examples/src/test/generated-assertions/org/assertj/Assertions.java index b070cdf6..7095b575 100644 --- a/assertions-examples/src/test/generated-assertions/org/assertj/Assertions.java +++ b/assertions-examples/src/test/generated-assertions/org/assertj/Assertions.java @@ -56,6 +56,16 @@ public static org.assertj.examples.data.AlignmentAssert assertThat(org.assertj.e return new org.assertj.examples.data.AlignmentAssert(actual); } + /** + * Creates a new instance of {@link org.assertj.examples.data.ArtWorkAssert}. + * + * @param actual the actual value. + * @return the created assertion object. + */ + public static org.assertj.examples.data.ArtWorkAssert assertThat(org.assertj.examples.data.ArtWork actual) { + return new org.assertj.examples.data.ArtWorkAssert(actual); + } + /** * Creates a new instance of {@link org.assertj.examples.data.BasketBallPlayerAssert}. * diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/BddAssertions.java b/assertions-examples/src/test/generated-assertions/org/assertj/BddAssertions.java index 33bb7105..eddec4d9 100644 --- a/assertions-examples/src/test/generated-assertions/org/assertj/BddAssertions.java +++ b/assertions-examples/src/test/generated-assertions/org/assertj/BddAssertions.java @@ -56,6 +56,16 @@ public static org.assertj.examples.data.AlignmentAssert then(org.assertj.example return new org.assertj.examples.data.AlignmentAssert(actual); } + /** + * Creates a new instance of {@link org.assertj.examples.data.ArtWorkAssert}. + * + * @param actual the actual value. + * @return the created assertion object. + */ + public static org.assertj.examples.data.ArtWorkAssert then(org.assertj.examples.data.ArtWork actual) { + return new org.assertj.examples.data.ArtWorkAssert(actual); + } + /** * Creates a new instance of {@link org.assertj.examples.data.BasketBallPlayerAssert}. * diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/JUnitSoftAssertions.java b/assertions-examples/src/test/generated-assertions/org/assertj/JUnitSoftAssertions.java index cd5a1bc6..78a95eb9 100644 --- a/assertions-examples/src/test/generated-assertions/org/assertj/JUnitSoftAssertions.java +++ b/assertions-examples/src/test/generated-assertions/org/assertj/JUnitSoftAssertions.java @@ -94,6 +94,16 @@ public org.assertj.examples.data.AlignmentAssert assertThat(org.assertj.examples return proxy(org.assertj.examples.data.AlignmentAssert.class, org.assertj.examples.data.Alignment.class, actual); } + /** + * Creates a new "soft" instance of {@link org.assertj.examples.data.ArtWorkAssert}. + * + * @param actual the actual value. + * @return the created "soft" assertion object. + */ + public org.assertj.examples.data.ArtWorkAssert assertThat(org.assertj.examples.data.ArtWork actual) { + return proxy(org.assertj.examples.data.ArtWorkAssert.class, org.assertj.examples.data.ArtWork.class, actual); + } + /** * Creates a new "soft" instance of {@link org.assertj.examples.data.BasketBallPlayerAssert}. * diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/SoftAssertions.java b/assertions-examples/src/test/generated-assertions/org/assertj/SoftAssertions.java index 880ace66..7e5453be 100644 --- a/assertions-examples/src/test/generated-assertions/org/assertj/SoftAssertions.java +++ b/assertions-examples/src/test/generated-assertions/org/assertj/SoftAssertions.java @@ -91,6 +91,16 @@ public org.assertj.examples.data.AlignmentAssert assertThat(org.assertj.examples return proxy(org.assertj.examples.data.AlignmentAssert.class, org.assertj.examples.data.Alignment.class, actual); } + /** + * Creates a new "soft" instance of {@link org.assertj.examples.data.ArtWorkAssert}. + * + * @param actual the actual value. + * @return the created "soft" assertion object. + */ + public org.assertj.examples.data.ArtWorkAssert assertThat(org.assertj.examples.data.ArtWork actual) { + return proxy(org.assertj.examples.data.ArtWorkAssert.class, org.assertj.examples.data.ArtWork.class, actual); + } + /** * Creates a new "soft" instance of {@link org.assertj.examples.data.BasketBallPlayerAssert}. * diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/AbstractArtWorkAssert.java b/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/AbstractArtWorkAssert.java new file mode 100644 index 00000000..95568cd3 --- /dev/null +++ b/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/AbstractArtWorkAssert.java @@ -0,0 +1,44 @@ +package org.assertj.examples.data; + +import org.assertj.core.api.AbstractAssert; +import org.assertj.core.util.Objects; + +/** + * Abstract base class for {@link ArtWork} specific assertions - Generated by CustomAssertionGenerator. + */ +public abstract class AbstractArtWorkAssert, A extends ArtWork> extends AbstractAssert { + + /** + * Creates a new {@link AbstractArtWorkAssert} to make assertions on actual ArtWork. + * @param actual the ArtWork we want to make assertions on. + */ + protected AbstractArtWorkAssert(A actual, Class selfType) { + super(actual, selfType); + } + + /** + * Verifies that the actual ArtWork's creator is equal to the given one. + * @param creator the given creator to compare the actual ArtWork's creator to. + * @return this assertion object. + * @throws AssertionError - if the actual ArtWork's creator is not equal to the given one. + */ + public S hasCreator(String creator) { + // check that actual ArtWork we want to make assertions on is not null. + isNotNull(); + + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting creator of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // check + String actualCreator = actual.getCreator(); + System.out.println("check Creator"); + if (!Objects.areEqual(actualCreator, creator)) { + failWithMessage(assertjErrorMessage, actual, creator, actualCreator); + } + + // return the current assertion for method chaining + return myself; + } + +} diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/ArtWorkAssert.java b/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/ArtWorkAssert.java new file mode 100644 index 00000000..a809e4c6 --- /dev/null +++ b/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/ArtWorkAssert.java @@ -0,0 +1,27 @@ +package org.assertj.examples.data; + + +/** + * {@link ArtWork} specific assertions - Generated by me. + * + * Although this class is not final to allow Soft assertions proxy, if you wish to extend it, + * extend {@link AbstractArtWorkAssert} instead. + */ +public class ArtWorkAssert extends AbstractArtWorkAssert { + + /** + * Creates a new {@link ArtWorkAssert} to make assertions on actual ArtWork. + * @param actual the ArtWork we want to make assertions on. + */ + public ArtWorkAssert(ArtWork actual) { + super(actual, ArtWorkAssert.class); + } + + /** + * factory method for ArtWork assertions. + * @return a new {@link ArtWorkAssert} + */ + public static ArtWorkAssert assertThat(ArtWork actual) { + return new ArtWorkAssert(actual); + } +} diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/movie/AbstractMovieAssert.java b/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/movie/AbstractMovieAssert.java index a9f8a77c..7d101e51 100644 --- a/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/movie/AbstractMovieAssert.java +++ b/assertions-examples/src/test/generated-assertions/org/assertj/examples/data/movie/AbstractMovieAssert.java @@ -1,12 +1,12 @@ package org.assertj.examples.data.movie; -import org.assertj.core.api.AbstractAssert; import org.assertj.core.util.Objects; +import org.assertj.examples.data.AbstractArtWorkAssert; /** * Abstract base class for {@link Movie} specific assertions - Generated by CustomAssertionGenerator. */ -public abstract class AbstractMovieAssert, A extends Movie> extends AbstractAssert { +public abstract class AbstractMovieAssert, A extends Movie> extends AbstractArtWorkAssert { /** * Creates a new {@link AbstractMovieAssert} to make assertions on actual Movie. diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/ItemAssert.java b/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/ItemAssert.java index 92098545..17e716e5 100644 --- a/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/ItemAssert.java +++ b/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/ItemAssert.java @@ -2,7 +2,7 @@ /** - * {@link Item} specific assertions - Generated by CustomAssertionGenerator. + * {@link Item} specific assertions - Generated by me. * * Although this class is not final to allow Soft assertions proxy, if you wish to extend it, * extend {@link AbstractItemAssert} instead. @@ -18,9 +18,7 @@ public ItemAssert(Item actual) { } /** - * An entry point for ItemAssert to follow AssertJ standard assertThat() statements.
- * With a static import, one can write directly: assertThat(myItem) and get specific assertion with code completion. - * @param actual the Item we want to make assertions on. + * factory method for Item assertions. * @return a new {@link ItemAssert} */ public static ItemAssert assertThat(Item actual) { diff --git a/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/WandAssert.java b/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/WandAssert.java index 5d807d86..592e9a05 100644 --- a/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/WandAssert.java +++ b/assertions-examples/src/test/generated-assertions/org/assertj/examples/rpg/WandAssert.java @@ -2,7 +2,7 @@ /** - * {@link Wand} specific assertions - Generated by CustomAssertionGenerator. + * {@link Wand} specific assertions - Generated by me. * * Although this class is not final to allow Soft assertions proxy, if you wish to extend it, * extend {@link AbstractWandAssert} instead. @@ -18,9 +18,7 @@ public WandAssert(Wand actual) { } /** - * An entry point for WandAssert to follow AssertJ standard assertThat() statements.
- * With a static import, one can write directly: assertThat(myWand) and get specific assertion with code completion. - * @param actual the Wand we want to make assertions on. + * factory method for Wand assertions. * @return a new {@link WandAssert} */ public static WandAssert assertThat(Wand actual) { diff --git a/assertions-examples/src/test/java/org/assertj/examples/GeneratedStandardAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/GeneratedStandardAssertionsExamples.java index 4644ffb6..43528826 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/GeneratedStandardAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/GeneratedStandardAssertionsExamples.java @@ -51,8 +51,10 @@ public void generated_bdd_assertions_example() throws NameException { assertThat(test).hasOnlyMovies(theFellowshipOfTheRing) .doesNotHaveMovies(theTwoTowers); + Movie movie = new Movie("boom", new Date(), "1h"); + movie.setCreator("foo"); + assertThat(movie).hasCreator("foo"); try { - Movie movie = new Movie("boom", new Date(), "1h"); movie = null; assertThat(movie).as("can be given ?").canBeGiven(); } catch (AssertionError e) { diff --git a/assertions-examples/src/test/java/org/assertj/examples/IterableAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/IterableAssertionsExamples.java index 4a216dd1..83c205bc 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/IterableAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/IterableAssertionsExamples.java @@ -12,6 +12,7 @@ */ package org.assertj.examples; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.extractProperty; import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; @@ -65,6 +66,9 @@ public class IterableAssertionsExamples extends AbstractAssertionsExamples { @Test public void iterable_basic_assertions_examples() { + List strings = asList("a", "b", "c"); + assertThat(strings).hasSize(3); + // would work the same way with Iterable, Iterable elvesRings = newArrayList(vilya, nenya, narya); assertThat(elvesRings).isNotEmpty().hasSize(3); diff --git a/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java index 2d896c6f..8bf3c7f2 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java @@ -13,9 +13,11 @@ package org.assertj.examples; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; import static org.assertj.core.api.Assertions.offset; import static org.assertj.core.api.Assertions.within; import static org.assertj.core.api.Assertions.withinPercentage; +import static org.assertj.core.data.Percentage.withPercentage; import java.math.BigDecimal; import java.math.BigInteger; @@ -206,4 +208,17 @@ public void should_consider_primitive_negative_zero_as_zero_fixing_issue_919() { assertThat(-0.).isZero(); } + @Test + public void should_handle_NaN_and_infinity_correctly_fixing_issue_984() { + assertThat(Double.NaN).isEqualTo(Double.NaN); + assertThat(Double.POSITIVE_INFINITY).isEqualTo(Double.POSITIVE_INFINITY); + assertThat(Double.NEGATIVE_INFINITY).isEqualTo(Double.NEGATIVE_INFINITY); + try { + assertThat(Double.NaN).isCloseTo(0.007, withPercentage(0.1)); + } catch (AssertionError e) { + return; + } + fail("assertThat(Double.NaN).isCloseTo(0.007, withPercentage(0.1)) should have failed"); + } + } diff --git a/assertions-examples/src/test/java/org/assertj/examples/StringAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/StringAssertionsExamples.java index 5357c360..8c3c4d57 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/StringAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/StringAssertionsExamples.java @@ -222,6 +222,27 @@ public void normalizing_newlines_equals_assertion() { assertThat("Game of Thrones\r\n").isEqualToNormalizingNewlines("Game of Thrones\n"); } + @Test + public void ignoring_whitespaces_equals_assertion() { + assertThat("Game of Thrones").isEqualToIgnoringWhitespace("Game of Thrones") + .isEqualToIgnoringWhitespace(" Game of Thrones ") + .isEqualToIgnoringWhitespace(" Game of Thrones ") + .isEqualToIgnoringWhitespace("Gameof Thrones") + .isEqualToIgnoringWhitespace("Game of\tThrones") + .isEqualToIgnoringWhitespace("GameofThrones"); + } + + @Test + public void normalizing_whitespaces_equals_assertion() { + assertThat("Game of Thrones").isEqualToNormalizingWhitespace("Game of Thrones") + .isEqualToNormalizingWhitespace("Game of Thrones") + .isEqualToNormalizingWhitespace("Game of Thrones") + .isEqualToNormalizingWhitespace(" Game of Thrones ") + .isEqualToNormalizingWhitespace(" Game of Thrones ") + .isEqualToNormalizingWhitespace("Game of\tThrones") + .isEqualToNormalizingWhitespace("Game of Thrones"); + } + @Test public void multine_collection_formatting() { String[] greatBooks = array("A Game of Thrones", "The Lord of the Rings", "Assassin's Apprentice ....", From 7de5ff263d174b1316f80dee66d2b461a4bae79e Mon Sep 17 00:00:00 2001 From: Joel Costigliola Date: Sun, 21 May 2017 15:33:52 +1200 Subject: [PATCH 2/6] Use assertj-core 2.8.1-SNAPSHOT --- assertions-examples/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assertions-examples/pom.xml b/assertions-examples/pom.xml index 5ed0fea3..1042465f 100644 --- a/assertions-examples/pom.xml +++ b/assertions-examples/pom.xml @@ -21,7 +21,7 @@ UTF-8 2.0.0 - 2.7.0-SNAPSHOT + 2.8.1-SNAPSHOT From ffc86798ac97b5489c7d2567bc33911296686c41 Mon Sep 17 00:00:00 2001 From: Joel Costigliola Date: Sun, 21 May 2017 15:40:06 +1200 Subject: [PATCH 3/6] Add isBetween and isStrictlyBetween examples for Temporal assertions --- .../Java8DateTimeAssertionsExamples.java | 97 ++++++++++++++++++- 1 file changed, 93 insertions(+), 4 deletions(-) diff --git a/assertions-examples/src/test/java/org/assertj/examples/Java8DateTimeAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/Java8DateTimeAssertionsExamples.java index ab7f7287..2067aff1 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/Java8DateTimeAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/Java8DateTimeAssertionsExamples.java @@ -61,11 +61,18 @@ public void zonedDateTime_assertions_examples() { .isBeforeOrEqualTo("2000-01-01T00:00:01Z") .isBeforeOrEqualTo("2000-01-01T00:00:00Z"); + assertThat(firstOfJanuary2000InUTC).isBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:01Z") + .isBetween("2000-01-01T00:00:00Z", "2000-01-01T00:00:01Z") + .isBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:00Z") + .isBetween("2000-01-01T00:00:00Z", "2000-01-01T00:00:00Z") + .isStrictlyBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:01Z"); + ZonedDateTime zonedDateTime1 = ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); ZonedDateTime zonedDateTime2 = ZonedDateTime.of(2000, 1, 1, 23, 59, 59, 999, ZoneOffset.UTC); ZonedDateTime zonedDateTime3 = ZonedDateTime.of(2000, 1, 1, 0, 59, 59, 999, ZoneOffset.UTC); ZonedDateTime zonedDateTime4 = ZonedDateTime.of(2000, 1, 1, 0, 0, 59, 999, ZoneOffset.UTC); ZonedDateTime zonedDateTime5 = ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 999, ZoneOffset.UTC); + assertThat(zonedDateTime1).isEqualToIgnoringHours(zonedDateTime2); assertThat(zonedDateTime1).isEqualToIgnoringMinutes(zonedDateTime3); assertThat(zonedDateTime1).isEqualToIgnoringSeconds(zonedDateTime4); @@ -78,6 +85,13 @@ public void zonedDateTime_assertions_examples() { } catch (AssertionError e) { logAssertionErrorMessage("isEqualToIgnoringHours with time zone change adjustment", e); } + + ZonedDateTime zonedDateTime = ZonedDateTime.now(); + assertThat(zonedDateTime).isBetween(zonedDateTime.minusSeconds(1), zonedDateTime.plusSeconds(1)) + .isBetween(zonedDateTime, zonedDateTime.plusSeconds(1)) + .isBetween(zonedDateTime.minusSeconds(1), zonedDateTime) + .isBetween(zonedDateTime, zonedDateTime) + .isStrictlyBetween(zonedDateTime.minusSeconds(1), zonedDateTime.plusSeconds(1)); } @Test @@ -90,14 +104,26 @@ public void instant_assertions_examples() { .isAfterOrEqualTo("2000-01-01T00:00:00.00Z") .isBefore(firstOfJanuary2000.plusSeconds(1)) .isBefore("2000-01-01T00:00:00.01Z") + .isBetween(firstOfJanuary2000.minusSeconds(1), firstOfJanuary2000.plusSeconds(1)) .isCloseTo("1999-12-31T23:59:59.99Z", within(10, ChronoUnit.MILLIS)) - .isCloseTo("1999-12-31T23:59:59.99Z", byLessThan(11, ChronoUnit.MILLIS)); - + .isCloseTo("1999-12-31T23:59:59.99Z", byLessThan(11, ChronoUnit.MILLIS)) + .isBetween("1999-01-01T00:00:00.00Z", "2001-01-01T00:00:00.00Z") + .isBetween("2000-01-01T00:00:00.00Z", "2001-01-01T00:00:00.00Z") + .isBetween("1999-01-01T00:00:00.00Z", "2000-01-01T00:00:00.00Z") + .isBetween("2000-01-01T00:00:00.00Z", "2000-01-01T00:00:00.00Z") + .isStrictlyBetween("1999-01-01T00:00:00.00Z", "2001-01-01T00:00:00.00Z"); try { assertThat(firstOfJanuary2000).isCloseTo("1999-12-31T23:59:59.99Z", within(1, ChronoUnit.MILLIS)); } catch (AssertionError e) { logAssertionErrorMessage("Instant.isCloseTo", e); } + + Instant instant = Instant.now(); + assertThat(instant).isBetween(instant.minusSeconds(1), instant.plusSeconds(1)) + .isBetween(instant, instant.plusSeconds(1)) + .isBetween(instant.minusSeconds(1), instant) + .isBetween(instant, instant) + .isStrictlyBetween(instant.minusSeconds(1), instant.plusSeconds(1)); } @Test @@ -114,6 +140,19 @@ public void localDateTime_assertions_examples() { .isBeforeOrEqualTo("2000-01-01T00:00:01") .isBeforeOrEqualTo("2000-01-01T00:00:00"); + assertThat(firstOfJanuary2000).isBetween("1999-12-31T23:59:59", "2000-01-01T00:01:01") + .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:01") + .isBetween("1999-12-31T23:59:59", "2000-01-01T00:00:00") + .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:00") + .isStrictlyBetween("1999-12-31T23:59:59", "2000-01-01T00:00:01"); + + LocalDateTime localDateTime = LocalDateTime.now(); + assertThat(localDateTime).isBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1)) + .isBetween(localDateTime, localDateTime.plusSeconds(1)) + .isBetween(localDateTime.minusSeconds(1), localDateTime) + .isBetween(localDateTime, localDateTime) + .isStrictlyBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1)); + // successful assertions ignoring ... // ... nanoseconds LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 0, 0, 1, 0); @@ -181,14 +220,26 @@ public void localDate_assertions_examples() { .isBeforeOrEqualTo("2000-01-02") .isBeforeOrEqualTo("2000-01-01"); + assertThat(firstOfJanuary2000).isBetween("1999-01-01", "2001-01-01") + .isBetween("2000-01-01", "2001-01-01") + .isBetween("1999-01-01", "2000-01-01") + .isBetween("2000-01-01", "2000-01-01") + .isStrictlyBetween("1999-01-01", "2001-01-01"); + assertThat(LocalDate.now()).isToday(); LocalTime _07_10 = LocalTime.of(7, 10); LocalTime _07_42 = LocalTime.of(7, 42); - // assertions will pass assertThat(_07_10).isCloseTo(_07_42, within(32, ChronoUnit.MINUTES)); assertThat(_07_10).isCloseTo(_07_42, within(1, ChronoUnit.HOURS)); + + LocalDate localDate = LocalDate.now(); + assertThat(localDate).isBetween(localDate.minusDays(1), localDate.plusDays(1)) + .isBetween(localDate, localDate.plusDays(1)) + .isBetween(localDate.minusDays(1), localDate) + .isBetween(localDate, localDate) + .isStrictlyBetween(localDate.minusDays(1), localDate.plusDays(1)); } @Test @@ -204,8 +255,21 @@ public void localTime_assertions_examples() { assertThat(oneAm).isBefore("02:00:00") .isBeforeOrEqualTo("02:00:00") .isBeforeOrEqualTo("01:00:00"); - + + assertThat(oneAm).isBetween("00:59:59", "01:00:01") + .isBetween("01:00:00", "01:00:01") + .isBetween("00:59:59", "01:00:00") + .isBetween("01:00:00", "01:00:00") + .isStrictlyBetween("00:59:59", "01:00:01"); + assertThat(LocalTime.parse("07:10:30")).isCloseTo("07:12:11", within(5, ChronoUnit.MINUTES)); + + LocalTime now = LocalTime.now(); + assertThat(now).isBetween(now.minusSeconds(1), now.plusSeconds(1)) + .isBetween(now, now.plusSeconds(1)) + .isBetween(now.minusSeconds(1), now) + .isBetween(now, now) + .isStrictlyBetween(now.minusSeconds(1), now.plusSeconds(1)); } @Test @@ -222,8 +286,20 @@ public void offsetTime_assertions_examples() { .isBeforeOrEqualTo("02:00:00+02:00") .isBeforeOrEqualTo("01:00:00+02:00"); + assertThat(oneAm).isBetween("00:59:59+02:00", "01:00:01+02:00") + .isBetween("01:00:00+02:00", "01:00:01+02:00") + .isBetween("00:59:59+02:00", "01:00:00+02:00") + .isBetween("01:00:00+02:00", "01:00:00+02:00") + .isStrictlyBetween("00:59:59+02:00", "01:00:01+02:00"); + assertThat(OffsetTime.parse("07:10:30+00:00")).isCloseTo("07:12:11+00:00", within(5, ChronoUnit.MINUTES)); + OffsetTime offsetTime = OffsetTime.now(); + assertThat(offsetTime).isBetween(offsetTime.minusSeconds(1), offsetTime.plusSeconds(1)) + .isBetween(offsetTime, offsetTime.plusSeconds(1)) + .isBetween(offsetTime.minusSeconds(1), offsetTime) + .isBetween(offsetTime, offsetTime) + .isStrictlyBetween(offsetTime.minusSeconds(1), offsetTime.plusSeconds(1)); } @Test @@ -252,6 +328,12 @@ public void offsetDateTime_assertions_examples() { .isBeforeOrEqualTo("2000-01-01T00:00:01Z") .isBeforeOrEqualTo("2000-01-01T00:00:00Z"); + assertThat(firstOfJanuary2000InUTC).isBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:01Z") + .isBetween("2000-01-01T00:00:00Z", "2000-01-01T00:00:01Z") + .isBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:00Z") + .isBetween("2000-01-01T00:00:00Z", "2000-01-01T00:00:00Z") + .isStrictlyBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:01Z"); + OffsetDateTime offsetDateTime1 = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); OffsetDateTime offsetDateTime2 = OffsetDateTime.of(2000, 1, 1, 23, 59, 59, 999, ZoneOffset.UTC); OffsetDateTime offsetDateTime3 = OffsetDateTime.of(2000, 1, 1, 0, 59, 59, 999, ZoneOffset.UTC); @@ -269,6 +351,13 @@ public void offsetDateTime_assertions_examples() { } catch (AssertionError e) { logAssertionErrorMessage("isEqualToIgnoringHours with time zone change adjustment", e); } + + OffsetDateTime offsetDateTime = OffsetDateTime.now(); + assertThat(offsetDateTime).isBetween(offsetDateTime.minusSeconds(1), offsetDateTime.plusSeconds(1)) + .isBetween(offsetDateTime, offsetDateTime.plusSeconds(1)) + .isBetween(offsetDateTime.minusSeconds(1), offsetDateTime) + .isBetween(offsetDateTime, offsetDateTime) + .isStrictlyBetween(offsetDateTime.minusSeconds(1), offsetDateTime.plusSeconds(1)); } } From 2af1f5d1a5fa4495f948e8ff5dd32de4f6d33a9d Mon Sep 17 00:00:00 2001 From: Joel Costigliola Date: Sun, 21 May 2017 15:40:38 +1200 Subject: [PATCH 4/6] Add returns assertion example --- .../java/org/assertj/examples/ObjectAssertionsExamples.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/assertions-examples/src/test/java/org/assertj/examples/ObjectAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/ObjectAssertionsExamples.java index 13e008b5..4e095d27 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/ObjectAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/ObjectAssertionsExamples.java @@ -76,7 +76,8 @@ Set getFoos() { @Test public void returns_assertion() { assertThat(frodo).returns("Frodo", from(TolkienCharacter::getName)) - .returns(HOBBIT, from(TolkienCharacter::getRace)); + .returns(HOBBIT, from(TolkienCharacter::getRace)) + .returns(HOBBIT, TolkienCharacter::getRace); } } From 55e022878686339a9f31b0ea396ba0bf940e0154 Mon Sep 17 00:00:00 2001 From: Joel Costigliola Date: Sun, 21 May 2017 15:40:59 +1200 Subject: [PATCH 5/6] use assertj-core 3.8.0-SNAPSHOT --- assertions-examples/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assertions-examples/pom.xml b/assertions-examples/pom.xml index 0e4c4bb5..ae8881bb 100644 --- a/assertions-examples/pom.xml +++ b/assertions-examples/pom.xml @@ -21,7 +21,7 @@ UTF-8 2.0.0 - 3.7.0-SNAPSHOT + 3.8.0-SNAPSHOT From 573ce0d64da2ad70eada84e1ccf092be67f2c4e1 Mon Sep 17 00:00:00 2001 From: Joel Costigliola Date: Sun, 21 May 2017 15:47:53 +1200 Subject: [PATCH 6/6] use assertj-core 3.8.1-SNAPSHOT --- assertions-examples/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assertions-examples/pom.xml b/assertions-examples/pom.xml index ae8881bb..6dc39781 100644 --- a/assertions-examples/pom.xml +++ b/assertions-examples/pom.xml @@ -21,7 +21,7 @@ UTF-8 2.0.0 - 3.8.0-SNAPSHOT + 3.8.1-SNAPSHOT