Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,12 @@ public void contains_exactly_for_primitive_types_assertion_examples() {

@Test
public void containsOnlyOnce_for_primitive_types_assertion_examples() {
// int
assertThat(new int[] { 1, 2, 3 }).containsOnlyOnce(1);
assertThat(new int[] { 1, 2, 3 }).containsOnlyOnce(1, 2);
assertThat(new int[] { 1, 2, 3 }).containsOnlyOnce(1, 2, 3);
assertThat(new int[] { 1, 2, 3 }).containsOnlyOnce(3, 2, 3);
assertThat(new int[] { 1, 2, 3 })
// int
.containsOnlyOnce(1)
.containsOnlyOnce(1, 2)
.containsOnlyOnce(1, 2, 3)
.containsOnlyOnce(3, 2, 3);
try {
assertThat(new int[] { 1, 2, 1 }).containsOnlyOnce(1);
} catch (AssertionError e) {
Expand Down Expand Up @@ -303,12 +304,13 @@ public void containsOnlyOnce_assertion_should_not_require_objects_to_be_comparab

@Test
public void hasSameSizeAs_assertion_examples() {
// comparing primitive arrays with primitive arrays
assertThat(new byte[] { 1, 2 }).hasSameSizeAs(new byte[] { 2, 3 });
assertThat(new byte[] { 1, 2 }).hasSameSizeAs(new int[] { 2, 3 });
// comparing primitive arrays with Object array
assertThat(new byte[] { 1, 2 }).hasSameSizeAs(new Byte[] { 2, 3 });
assertThat(new byte[] { 1, 2 }).hasSameSizeAs(new String[] { "1", "2" });
assertThat(new byte[] { 1, 2 })
// comparing primitive arrays with primitive arrays
.hasSameSizeAs(new byte[] { 2, 3 })
.hasSameSizeAs(new int[] { 2, 3 })
// comparing primitive arrays with Object array
.hasSameSizeAs(new Byte[] { 2, 3 })
.hasSameSizeAs(new String[] { "1", "2" });
// comparing primitive arrays with Iterable
assertThat(new long[] { 1, 2, 3 }).hasSameSizeAs(newArrayList(vilya, nenya, narya));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ public void should_be_able_to_extract_primitive_values() {

@Test
public void has_field_or_property_examples() {
assertThat(frodo).hasFieldOrProperty("age");
// private field is found unless Assertions.setAllowExtractingPrivateFields(false);
assertThat(frodo).hasFieldOrProperty("notAccessibleField");
assertThat(frodo)
.hasFieldOrProperty("age")
// private field is found unless Assertions.setAllowExtractingPrivateFields(false);
.hasFieldOrProperty("notAccessibleField");
assertThat(frodo)
.hasFieldOrPropertyWithValue("age", 33)
.hasFieldOrProperty("race.name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public void comparable_assertions_examples() {

Rating goodRating = new Rating(8);
Rating badRating = new Rating(4);
assertThat(goodRating).isGreaterThan(badRating);
assertThat(goodRating).isGreaterThanOrEqualTo(badRating);
assertThat(badRating).isLessThan(goodRating);
assertThat(badRating).isLessThanOrEqualTo(goodRating);
assertThat(goodRating).isEqualByComparingTo(goodRating);
assertThat(goodRating).isNotEqualByComparingTo(badRating);
assertThat(goodRating)
.isGreaterThan(badRating)
.isGreaterThanOrEqualTo(badRating);
assertThat(badRating)
.isLessThan(goodRating)
.isLessThanOrEqualTo(goodRating);
assertThat(goodRating)
.isEqualByComparingTo(goodRating)
.isNotEqualByComparingTo(badRating);
try {
assertThat(badRating).isGreaterThan(goodRating);
} catch (AssertionError e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@ public void date_assertions_comparison_with_precision_level() {
Date date4 = parseDatetimeWithMs("2003-01-01T01:55:55.555");
Date date5 = parseDatetimeWithMs("2003-01-01T05:55:55.555");

assertThat(date1).isEqualToIgnoringMillis(date2);
assertThat(date1).isInSameSecondAs(date2);
assertThat(date1).isEqualToIgnoringSeconds(date3);
assertThat(date1).isInSameMinuteAs(date3);
assertThat(date1).isEqualToIgnoringMinutes(date4);
assertThat(date1).isInSameHourAs(date4);
assertThat(date1).isEqualToIgnoringHours(date5);
assertThat(date1).isInSameDayAs(date5);
assertThat(date1)
.isEqualToIgnoringMillis(date2)
.isInSameSecondAs(date2)
.isEqualToIgnoringSeconds(date3)
.isInSameMinuteAs(date3)
.isEqualToIgnoringMinutes(date4)
.isInSameHourAs(date4)
.isEqualToIgnoringHours(date5)
.isInSameDayAs(date5);

try {
assertThat(date1).isEqualToIgnoringMillis(date3);
Expand Down Expand Up @@ -143,9 +144,10 @@ public void is_in_the_same_second_assertions_examples() {
logAssertionErrorMessage("isInSameSecondAs date assertion", e);
}

// succeeds because the time difference between dates < 1s
assertThat(date1).isInSameSecondWindowAs(date2);
assertThat(date1).isInSameSecondWindowAs(date3);
assertThat(date1)
// succeeds because the time difference between dates < 1s
.isInSameSecondWindowAs(date2)
.isInSameSecondWindowAs(date3);
// fails because time difference between dates >= 1s
try {
assertThat(date1).isInSameSecondWindowAs(parseDatetimeWithMs("2003-01-01T12:00:02.000"));
Expand All @@ -171,9 +173,10 @@ public void is_in_the_same_minute_assertions_examples() {
logAssertionErrorMessage("isInSameMinuteWindowAs date assertion", e);
}

// succeeds because date time difference < 1 min
assertThat(date1).isInSameMinuteWindowAs(date2);
assertThat(date1).isInSameMinuteWindowAs(date3);
assertThat(date1)
// succeeds because date time difference < 1 min
.isInSameMinuteWindowAs(date2)
.isInSameMinuteWindowAs(date3);
try {
// fails because date time difference >= 1 min
assertThat(date1).isInSameMinuteWindowAs(parseDatetime("2003-01-01T12:02:00"));
Expand All @@ -199,9 +202,10 @@ public void is_in_the_same_hour_assertions_examples() {
logAssertionErrorMessage("isInSameHourAs date assertion", e);
}

// succeeds because time difference < 1h
assertThat(date1).isInSameHourWindowAs(date2);
assertThat(date1).isInSameHourWindowAs(date3);
assertThat(date1)
// succeeds because time difference < 1h
.isInSameHourWindowAs(date2)
.isInSameHourWindowAs(date3);
try {
assertThat(date1).isInSameHourWindowAs(date4);
} catch (AssertionError e) {
Expand All @@ -213,21 +217,23 @@ public void is_in_the_same_hour_assertions_examples() {
public void is_in_the_same_day_or_month_or_year_assertions_examples() {
Date date1 = parseDatetime("2003-04-26T23:17:00");
Date date2 = parseDatetime("2003-04-26T12:30:00");
assertThat(date1).isInSameDayAs(date2);
assertThat(date1).isInSameMonthAs("2003-04-27");
assertThat(date1).isInSameYearAs("2003-05-13");
assertThat(date1)
.isInSameDayAs(date2)
.isInSameMonthAs("2003-04-27")
.isInSameYearAs("2003-05-13");
}

@Test
public void has_field_date_assertions_examples() {
Date date1 = parseDatetimeWithMs("2003-04-26T13:20:35.017");
assertThat(date1).hasMillisecond(17);
assertThat(date1).hasSecond(35);
assertThat(date1).hasMinute(20);
assertThat(date1).hasHourOfDay(13);
assertThat(date1).hasDayOfWeek(Calendar.SATURDAY);
assertThat(date1).hasMonth(4);
assertThat(date1).hasYear(2003);
assertThat(date1)
.hasMillisecond(17)
.hasSecond(35)
.hasMinute(20)
.hasHourOfDay(13)
.hasDayOfWeek(Calendar.SATURDAY)
.hasMonth(4)
.hasYear(2003);
}

@Test
Expand Down Expand Up @@ -304,12 +310,14 @@ public void lenient_date_parsing() {

Assertions.setLenientDateParsing(true);

// assertions will pass
assertThat(date).isEqualTo("2001-01-34");
assertThat(date).isEqualTo("2001-02-02T24:00:00");
assertThat(date).isEqualTo("2001-02-04T-24:00:00.000");
assertThat(dateTime).isEqualTo("2001-02-03T04:05:05.1000");
assertThat(dateTime).isEqualTo("2001-02-03T04:04:66");
assertThat(date)
// assertions will pass
.isEqualTo("2001-01-34")
.isEqualTo("2001-02-02T24:00:00")
.isEqualTo("2001-02-04T-24:00:00.000");
assertThat(dateTime)
.isEqualTo("2001-02-03T04:05:05.1000")
.isEqualTo("2001-02-03T04:04:66");
assertThat(dateTimeWithMs).isEqualTo("2001-02-03T04:05:07.-300");

// assertions will fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public void file_assertions_examples() throws Exception {

// compare content with another file
File xFileClone = writeFile("xFileClone", "The Truth Is Out There");
assertThat(xFile).hasSameTextualContentAs(xFileClone);
assertThat(xFile).hasSameBinaryContentAs(xFileClone);
assertThat(xFile)
.hasSameTextualContentAs(xFileClone)
.hasSameBinaryContentAs(xFileClone);

// compare content with a string
assertThat(xFile).isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public class Java8DateTimeAssertionsExamples extends AbstractAssertionsExamples
public void zonedDateTime_assertions_examples() {

ZonedDateTime firstOfJanuary2000InUTC = ZonedDateTime.parse("2000-01-01T00:00:00Z");
assertThat(firstOfJanuary2000InUTC).isEqualTo(ZonedDateTime.parse("2000-01-01T00:00:00Z"));
// same assertion but AssertJ takes care of expected String to ZonedDateTime conversion
assertThat(firstOfJanuary2000InUTC).isEqualTo("2000-01-01T00:00:00Z");
// example showing that ZonedDateTime are compared in actual's time zone
assertThat(firstOfJanuary2000InUTC).isEqualTo("2000-01-01T01:00:00+01:00");
assertThat(firstOfJanuary2000InUTC)
.isEqualTo(ZonedDateTime.parse("2000-01-01T00:00:00Z"))
// same assertion but AssertJ takes care of expected String to ZonedDateTime conversion
.isEqualTo("2000-01-01T00:00:00Z")
// example showing that ZonedDateTime are compared in actual's time zone
.isEqualTo("2000-01-01T01:00:00+01:00");

try {
// 2000-01-01T00:00+01:00 = 1999-12-31T23:00:00Z !
Expand Down Expand Up @@ -73,10 +74,11 @@ public void zonedDateTime_assertions_examples() {
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);
assertThat(zonedDateTime1).isEqualToIgnoringNanos(zonedDateTime5);
assertThat(zonedDateTime1)
.isEqualToIgnoringHours(zonedDateTime2)
.isEqualToIgnoringMinutes(zonedDateTime3)
.isEqualToIgnoringSeconds(zonedDateTime4)
.isEqualToIgnoringNanos(zonedDateTime5);

// example showing that ZonedDateTime are compared in actual's time zone
ZonedDateTime zonedDateTimeNotInUTC = ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC+1"));
Expand Down Expand Up @@ -233,8 +235,9 @@ public void localDate_assertions_examples() {
LocalTime _07_10 = LocalTime.of(7, 10);
LocalTime _07_42 = LocalTime.of(7, 42);

assertThat(_07_10).isCloseTo(_07_42, within(32, ChronoUnit.MINUTES));
assertThat(_07_10).isCloseTo(_07_42, within(1, ChronoUnit.HOURS));
assertThat(_07_10)
.isCloseTo(_07_42, within(32, ChronoUnit.MINUTES))
.isCloseTo(_07_42, within(1, ChronoUnit.HOURS));

LocalDate localDate = LocalDate.now();
assertThat(localDate).isBetween(localDate.minusDays(1), localDate.plusDays(1))
Expand Down Expand Up @@ -308,9 +311,10 @@ public void offsetTime_assertions_examples() {
public void offsetDateTime_assertions_examples() {

OffsetDateTime firstOfJanuary2000InUTC = OffsetDateTime.parse("2000-01-01T00:00:00Z");
assertThat(firstOfJanuary2000InUTC).isEqualTo(OffsetDateTime.parse("2000-01-01T00:00:00Z"));
// same assertion but AssertJ takes care of expected String to OffsetDateTime conversion
assertThat(firstOfJanuary2000InUTC).isEqualTo("2000-01-01T00:00:00Z");
assertThat(firstOfJanuary2000InUTC)
.isEqualTo(OffsetDateTime.parse("2000-01-01T00:00:00Z"))
// same assertion but AssertJ takes care of expected String to OffsetDateTime conversion
.isEqualTo("2000-01-01T00:00:00Z");

try {
// 2000-01-01T00:00+01:00 = 1999-12-31T23:00:00Z !
Expand Down Expand Up @@ -341,10 +345,11 @@ public void offsetDateTime_assertions_examples() {
OffsetDateTime offsetDateTime3 = OffsetDateTime.of(2000, 1, 1, 0, 59, 59, 999, ZoneOffset.UTC);
OffsetDateTime offsetDateTime4 = OffsetDateTime.of(2000, 1, 1, 0, 0, 59, 999, ZoneOffset.UTC);
OffsetDateTime offsetDateTime5 = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 999, ZoneOffset.UTC);
assertThat(offsetDateTime1).isEqualToIgnoringHours(offsetDateTime2);
assertThat(offsetDateTime1).isEqualToIgnoringMinutes(offsetDateTime3);
assertThat(offsetDateTime1).isEqualToIgnoringSeconds(offsetDateTime4);
assertThat(offsetDateTime1).isEqualToIgnoringNanos(offsetDateTime5);
assertThat(offsetDateTime1)
.isEqualToIgnoringHours(offsetDateTime2)
.isEqualToIgnoringMinutes(offsetDateTime3)
.isEqualToIgnoringSeconds(offsetDateTime4)
.isEqualToIgnoringNanos(offsetDateTime5);

// example showing that OffsetDateTime are compared in actual's time zone
OffsetDateTime offsetDateTimeNotInUTC = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneOffset.MAX);
Expand Down
Loading