How to use haveSameYear method of org.assertj.core.api.AbstractLocalDateTimeAssert class

Best Assertj code snippet using org.assertj.core.api.AbstractLocalDateTimeAssert.haveSameYear

Source:AbstractLocalDateTimeAssert.java Github

copy

Full Screen

...570 */571 public SELF isEqualToIgnoringHours(LocalDateTime other) {572 Objects.instance().assertNotNull(info, actual);573 assertLocalDateTimeParameterIsNotNull(other);574 if (!haveSameYearMonthAndDayOfMonth(actual, other)) {575 throw Failures.instance().failure(info, shouldBeEqualIgnoringHours(actual, other));576 }577 return myself;578 }579 /**580 * Verifies that the actual {@link LocalDateTime} is in the [start, end] period (start and end included) according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator.581 * <p>582 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.583 * <p>584 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.585 * <p>586 * Example:587 * <pre><code class='java'> LocalDateTime localDateTime = LocalDateTime.now();588 *589 * // assertions succeed:590 * assertThat(localDateTime).isBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1))591 * .isBetween(localDateTime, localDateTime.plusSeconds(1))592 * .isBetween(localDateTime.minusSeconds(1), localDateTime)593 * .isBetween(localDateTime, localDateTime);594 *595 * // assertions fail:596 * assertThat(localDateTime).isBetween(localDateTime.minusSeconds(10), localDateTime.minusSeconds(1));597 * assertThat(localDateTime).isBetween(localDateTime.plusSeconds(1), localDateTime.plusSeconds(10));</code></pre>598 *599 * @param startInclusive the start value (inclusive), expected not to be null.600 * @param endInclusive the end value (inclusive), expected not to be null.601 * @return this assertion object.602 * @throws AssertionError if the actual value is {@code null}.603 * @throws NullPointerException if start value is {@code null}.604 * @throws NullPointerException if end value is {@code null}.605 * @throws AssertionError if the actual value is not in [start, end] period.606 *607 * @since 3.7.1608 */609 public SELF isBetween(LocalDateTime startInclusive, LocalDateTime endInclusive) {610 comparables.assertIsBetween(info, actual, startInclusive, endInclusive, true, true);611 return myself;612 }613 /**614 * Same assertion as {@link #isBetween(LocalDateTime, LocalDateTime)} but here you pass {@link LocalDateTime} String representations615 * which must follow <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME">ISO LocalDateTime format</a>616 * to allow calling {@link LocalDateTime#parse(CharSequence)} method.617 * <p>618 * Example:619 * <pre><code class='java'> LocalDateTime firstOfJanuary2000 = LocalDateTime.parse("2000-01-01T00:00:00");620 *621 * // assertions succeed:622 * assertThat(firstOfJanuary2000).isBetween("1999-12-31T23:59:59", "2000-01-01T00:00:01")623 * .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:01")624 * .isBetween("1999-12-31T23:59:59", "2000-01-01T00:00:00")625 * .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:00");626 *627 * // assertion fails:628 * assertThat(firstOfJanuary2000).isBetween("1999-01-01T00:00:01", "1999-12-31T23:59:59");</code></pre>629 *630 * @param startInclusive the start value (inclusive), expected not to be null.631 * @param endInclusive the end value (inclusive), expected not to be null.632 * @return this assertion object.633 *634 * @throws AssertionError if the actual value is {@code null}.635 * @throws NullPointerException if start value is {@code null}.636 * @throws NullPointerException if end value is {@code null}.637 * @throws DateTimeParseException if any of the given String can't be converted to a {@link LocalDateTime}.638 * @throws AssertionError if the actual value is not in [start, end] period.639 *640 * @since 3.7.1641 */642 public SELF isBetween(String startInclusive, String endInclusive) {643 return isBetween(parse(startInclusive), parse(endInclusive));644 }645 /**646 * Verifies that the actual {@link LocalDateTime} is in the ]start, end[ period (start and end excluded) according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator.647 * <p>648 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.649 * <p>650 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.651 * <p>652 * Example:653 * <pre><code class='java'> LocalDateTime localDateTime = LocalDateTime.now();654 *655 * // assertion succeeds:656 * assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1));657 *658 * // assertions fail:659 * assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(10), localDateTime.minusSeconds(1));660 * assertThat(localDateTime).isStrictlyBetween(localDateTime.plusSeconds(1), localDateTime.plusSeconds(10));661 * assertThat(localDateTime).isStrictlyBetween(localDateTime, localDateTime.plusSeconds(1));662 * assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(1), localDateTime);</code></pre>663 *664 * @param startExclusive the start value (exclusive), expected not to be null.665 * @param endExclusive the end value (exclusive), expected not to be null.666 * @return this assertion object.667 * @throws AssertionError if the actual value is {@code null}.668 * @throws NullPointerException if start value is {@code null}.669 * @throws NullPointerException if end value is {@code null}.670 * @throws AssertionError if the actual value is not in ]start, end[ period.671 *672 * @since 3.7.1673 */674 public SELF isStrictlyBetween(LocalDateTime startExclusive, LocalDateTime endExclusive) {675 comparables.assertIsBetween(info, actual, startExclusive, endExclusive, false, false);676 return myself;677 }678 /**679 * Same assertion as {@link #isStrictlyBetween(LocalDateTime, LocalDateTime)} but here you pass {@link LocalDateTime} String representations680 * which must follow <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME">ISO LocalDateTime format</a>681 * to allow calling {@link LocalDateTime#parse(CharSequence)} method.682 * <p>683 * Example:684 * <pre><code class='java'> LocalDateTime firstOfJanuary2000 = LocalDateTime.parse("2000-01-01T00:00:00");685 *686 * // assertion succeeds:687 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59", "2000-01-01T00:00:01");688 *689 * // assertions fail:690 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-01-01T00:00:01", "1999-12-31T23:59:59");691 * assertThat(firstOfJanuary2000).isStrictlyBetween("2000-01-01T00:00:00", "2000-01-01T00:00:01");692 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59", "2000-01-01T00:00:00");</code></pre>693 *694 * @param startExclusive the start value (exclusive), expected not to be null.695 * @param endExclusive the end value (exclusive), expected not to be null.696 * @return this assertion object.697 *698 * @throws AssertionError if the actual value is {@code null}.699 * @throws NullPointerException if start value is {@code null}.700 * @throws NullPointerException if end value is {@code null}.701 * @throws DateTimeParseException if any of the given String can't be converted to a {@link LocalDateTime}.702 * @throws AssertionError if the actual value is not in ]start, end[ period.703 *704 * @since 3.7.1705 */706 public SELF isStrictlyBetween(String startExclusive, String endExclusive) {707 return isStrictlyBetween(parse(startExclusive), parse(endExclusive));708 }709 /**710 * {@inheritDoc}711 */712 @Override713 protected LocalDateTime parse(String localDateTimeAsString) {714 return LocalDateTime.parse(localDateTimeAsString);715 }716 /**717 * Returns true if both datetime are in the same year, month and day of month, hour, minute and second, false718 * otherwise.719 *720 * @param actual the actual datetime. expected not be null721 * @param other the other datetime. expected not be null722 * @return true if both datetime are in the same year, month and day of month, hour, minute and second, false723 * otherwise.724 */725 private static boolean areEqualIgnoringNanos(LocalDateTime actual, LocalDateTime other) {726 return areEqualIgnoringSeconds(actual, other) && actual.getSecond() == other.getSecond();727 }728 /**729 * Returns true if both datetime are in the same year, month, day of month, hour and minute, false otherwise.730 *731 * @param actual the actual datetime. expected not be null732 * @param other the other datetime. expected not be null733 * @return true if both datetime are in the same year, month, day of month, hour and minute, false otherwise.734 */735 private static boolean areEqualIgnoringSeconds(LocalDateTime actual, LocalDateTime other) {736 return areEqualIgnoringMinutes(actual, other) && actual.getMinute() == other.getMinute();737 }738 /**739 * Returns true if both datetime are in the same year, month, day of month and hour, false otherwise.740 *741 * @param actual the actual datetime. expected not be null742 * @param other the other datetime. expected not be null743 * @return true if both datetime are in the same year, month, day of month and hour, false otherwise.744 */745 private static boolean areEqualIgnoringMinutes(LocalDateTime actual, LocalDateTime other) {746 return haveSameYearMonthAndDayOfMonth(actual, other) && actual.getHour() == other.getHour();747 }748 /**749 * Returns true if both datetime are in the same year, month and day of month, false otherwise.750 *751 * @param actual the actual datetime. expected not be null752 * @param other the other datetime. expected not be null753 * @return true if both datetime are in the same year, month and day of month, false otherwise754 */755 private static boolean haveSameYearMonthAndDayOfMonth(LocalDateTime actual, LocalDateTime other) {756 return haveSameYearAndMonth(actual, other) && actual.getDayOfMonth() == other.getDayOfMonth();757 }758 /**759 * Returns true if both datetime are in the same year and month, false otherwise.760 *761 * @param actual the actual datetime. expected not be null762 * @param other the other datetime. expected not be null763 * @return true if both datetime are in the same year and month, false otherwise764 */765 private static boolean haveSameYearAndMonth(LocalDateTime actual, LocalDateTime other) {766 return haveSameYear(actual, other) && actual.getMonth() == other.getMonth();767 }768 /**769 * Returns true if both datetime are in the same year, false otherwise.770 *771 * @param actual the actual datetime. expected not be null772 * @param other the other datetime. expected not be null773 * @return true if both datetime are in the same year, false otherwise774 */775 private static boolean haveSameYear(LocalDateTime actual, LocalDateTime other) {776 return actual.getYear() == other.getYear();777 }778}...

Full Screen

Full Screen

haveSameYear

Using AI Code Generation

copy

Full Screen

1LocalDateTime date = LocalDateTime.of(2018, 10, 1, 0, 0);2LocalDateTime date2 = LocalDateTime.of(2018, 11, 1, 0, 0);3assertThat(date).hasSameYearAs(date2);4assertThat(date).hasSameYearAs("2018-11-01T00:00:00");5LocalDateTime date = LocalDateTime.of(2018, 10, 1, 0, 0);6LocalDateTime date2 = LocalDateTime.of(2018, 11, 1, 0, 0);7assertThat(date).hasSameMonthAs(date2);8assertThat(date).hasSameMonthAs("2018-11-01T00:00:00");9LocalDateTime date = LocalDateTime.of(2018, 10, 1, 0, 0);10LocalDateTime date2 = LocalDateTime.of(2018, 10, 2, 0, 0);11assertThat(date).hasSameDayAs(date2);12assertThat(date).hasSameDayAs("2018-10-02T00:00:00");13LocalDateTime date = LocalDateTime.of(2018, 10, 1, 10, 0);14LocalDateTime date2 = LocalDateTime.of(2018, 10, 1, 11, 0);15assertThat(date).hasSameHourAs(date2);16assertThat(date).hasSameHourAs("2018-10-01T11:00:00");17LocalDateTime date = LocalDateTime.of(2018, 10, 1, 10, 0);18LocalDateTime date2 = LocalDateTime.of(2018, 10, 1, 10, 1);19assertThat(date).hasSameMinuteAs(date2);20assertThat(date).hasSameMinuteAs("2018-10-01T10:01:00");21LocalDateTime date = LocalDateTime.of(2018, 10,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful