Best Assertj code snippet using org.assertj.core.error.ShouldHavePeriod.shouldHaveYears
Source:AbstractPeriodAssert.java
...14import static org.assertj.core.error.ShouldBePeriod.shouldBeNegative;15import static org.assertj.core.error.ShouldBePeriod.shouldBePositive;16import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;17import static org.assertj.core.error.ShouldHavePeriod.shouldHaveMonths;18import static org.assertj.core.error.ShouldHavePeriod.shouldHaveYears;19import java.time.Period;20import org.assertj.core.internal.Failures;21/**22 * Assertions for {@link Period} type.23 * @author Hayden Meloche24 * @since 3.17.025 */26public abstract class AbstractPeriodAssert<SELF extends AbstractPeriodAssert<SELF>> extends AbstractAssert<SELF, Period> {27 /**28 * Creates a new <code>{@link org.assertj.core.api.AbstractPeriodAssert}</code>29 * @param period the actual value to verify30 * @param selfType the "self type"31 */32 protected AbstractPeriodAssert(Period period, Class<?> selfType) {33 super(period, selfType);34 }35 /**36 * Verifies that the actual {@code Period} has the given years.37 * <p>38 * Example :39 * <pre><code class='java'> // assertion will pass40 * assertThat(Period.ofYears(5)).hasYears(5);41 *42 * // assertion will fail43 * assertThat(Period.ofYears(5)).hasYears(1);</code></pre>44 *45 * @param expectedYears the expected years value46 * @return this assertion object47 * @throws AssertionError if the actual {@code Period} is {@code null}48 * @throws AssertionError if the actual {@code Period} does not have the given years49 * @since 3.17.050 */51 public SELF hasYears(int expectedYears) {52 isNotNull();53 int actualYears = actual.getYears();54 if (expectedYears != actualYears) {55 throw Failures.instance().failure(info, shouldHaveYears(actual, actualYears, expectedYears), actualYears, expectedYears);56 }57 return myself;58 }59 /**60 * Verifies that the actual {@code Period} has the given months.61 * <p>62 * Example :63 * <pre><code class='java'> // assertion will pass64 * assertThat(Period.ofMonths(5)).hasMonths(5);65 *66 * // assertion will fail67 * assertThat(Period.ofMonths(5)).hasMonths(1);</code></pre>68 *69 * @param expectedMonths the expected months value...
Source:ShouldHavePeriod_create_test.java
...14import static java.lang.String.format;15import static org.assertj.core.api.BDDAssertions.then;16import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;17import static org.assertj.core.error.ShouldHavePeriod.shouldHaveMonths;18import static org.assertj.core.error.ShouldHavePeriod.shouldHaveYears;19import java.time.Period;20import org.junit.jupiter.api.Test;21/**22 * @author Hayden Meloche23 */24class ShouldHavePeriod_create_test {25 @Test26 void should_create_error_message_for_years() {27 // GIVEN28 int actualYears = 1;29 int expectedYears = 2;30 Period period = Period.ofYears(actualYears);31 // WHEN32 String errorMessage = shouldHaveYears(period, actualYears, expectedYears).create();33 // THEN34 then(errorMessage).isEqualTo(format("%nExpecting Period:%n P1Y%nto have 2 years but had 1"));35 }36 @Test37 void should_create_error_message_for_1_year() {38 // GIVEN39 int actualYears = 2;40 int expectedYears = 1;41 Period period = Period.ofYears(actualYears);42 // WHEN43 String errorMessage = shouldHaveYears(period, actualYears, expectedYears).create();44 // THEN45 then(errorMessage).isEqualTo(format("%nExpecting Period:%n P2Y%nto have 1 year but had 2"));46 }47 @Test48 void should_create_error_message_for_negative_1_year() {49 // GIVEN50 int actualYears = 2;51 int expectedYears = -1;52 Period period = Period.ofYears(actualYears);53 // WHEN54 String errorMessage = shouldHaveYears(period, actualYears, expectedYears).create();55 // THEN56 then(errorMessage).isEqualTo(format("%nExpecting Period:%n P2Y%nto have -1 year but had 2"));57 }58 @Test59 void should_create_error_message_for_days() {60 // GIVEN61 int actualDays = 1;62 int expectedDays = 2;63 Period period = Period.ofDays(actualDays);64 // WHEN65 String errorMessage = shouldHaveDays(period, actualDays, expectedDays).create();66 // THEN67 then(errorMessage).isEqualTo(format("%nExpecting Period:%n P1D%nto have 2 days but had 1"));68 }...
shouldHaveYears
Using AI Code Generation
1assertThat(Period.ofYears(1)).shouldHaveYears(1);2assertThat(Period.ofMonths(1)).shouldHaveMonths(1);3assertThat(Period.ofDays(1)).shouldHaveDays(1);4assertThat(Period.ofDays(1)).shouldHaveDays(1);5assertThat(Period.of(1, 1, 1)).shouldHave(1, 1, 1);6assertThat(Period.ofYears(1)).shouldHaveYears(1);7assertThat(Period.ofMonths(1)).shouldHaveMonths(1);8assertThat(Period.ofDays(1)).shouldHaveDays(1);9assertThat(Period.ofDays(1)).shouldHaveDays(1);10assertThat(Period.of(1, 1, 1)).shouldHave(1, 1, 1);11assertThat(Period.ofYears(1)).shouldHaveYears(1);12assertThat(Period.ofMonths(1)).shouldHaveMonths(1);13assertThat(Period.ofDays(1)).shouldHaveDays(1);14assertThat(Period.ofDays(1)).shouldHaveDays(1);15assertThat(Period.of
shouldHaveYears
Using AI Code Generation
1ShouldHavePeriod.shouldHaveYears(actual, expected);2ShouldHavePeriod.shouldHaveMonths(actual, expected);3ShouldHavePeriod.shouldHaveDays(actual, expected);4ShouldHavePeriod.shouldHaveHours(actual, expected);5ShouldHavePeriod.shouldHaveMinutes(actual, expected);6ShouldHavePeriod.shouldHaveSeconds(actual, expected);7ShouldHavePeriod.shouldHaveNanos(actual, expected);8ShouldHavePeriod.shouldHaveDays(actual, expected);9ShouldHavePeriod.shouldHaveHours(actual, expected);10ShouldHavePeriod.shouldHaveMinutes(actual, expected);11ShouldHavePeriod.shouldHaveSeconds(actual, expected);12ShouldHavePeriod.shouldHaveNanos(actual, expected);13ShouldHavePeriod.shouldHaveDays(actual, expected);14ShouldHavePeriod.shouldHaveHours(actual, expected);
shouldHaveYears
Using AI Code Generation
1 public void test1() {2 assertThatThrownBy(() -> assertThat(Period.of(1, 0, 0)).shouldHaveYears(2))3 .isInstanceOf(AssertionError.class)4 .hasMessage(format("%n" +5 " <1>"));6 }7 public void test2() {8 assertThatThrownBy(() -> assertThat(Period.of(0, 1, 0)).shouldHaveMonths(2))9 .isInstanceOf(AssertionError.class)10 .hasMessage(format("%n" +11 " <1>"));12 }13 public void test3() {14 assertThatThrownBy(() -> assertThat(Period.of(0, 0, 1)).shouldHaveDays(2))15 .isInstanceOf(AssertionError.class)16 .hasMessage(format("%n" +17 " <1>"));18 }19 public void test4() {20 assertThatThrownBy(() -> assertThat(Period.of(1, 0, 0)).shouldNotHaveYears(1))21 .isInstanceOf(AssertionError.class)22 .hasMessage(format("%n" +23 " <1>"));24 }
shouldHaveYears
Using AI Code Generation
1Period period = Period.of(1, 2, 3);2assertThat(period).shouldHaveYears(1);3Period period = Period.of(1, 2, 3);4assertThat(period).shouldHaveMonths(2);5Period period = Period.of(1, 2, 3);6assertThat(period).shouldHaveDays(3);7Period period = Period.of(1, 2, 3);8assertThat(period).shouldHaveNoYears();9Period period = Period.of(1, 2, 3);10assertThat(period).shouldHaveNoMonths();11Period period = Period.of(1, 2, 3);12assertThat(period).shouldHaveNoDays();13Period period = Period.of(-1, 2, 3);14assertThat(period).shouldHaveNegativeYears();15Period period = Period.of(1, -2, 3);16assertThat(period).shouldHaveNegativeMonths();17Period period = Period.of(1, 2, -3);18assertThat(period).shouldHaveNegativeDays();
shouldHaveYears
Using AI Code Generation
1package com.example;2import org.junit.Test;3import org.junit.runner.RunWith;4import org.junit.runners.JUnit4;5import static org.assertj.core.api.Assertions.*;6import java.time.Period;7@RunWith(JUnit4.class)8public class AssertJPeriodTest {9 public void testAssertJPeriod() {10 Period period = Period.of(1, 1, 1);11 Period period2 = Period.of(2, 2, 2);12 assertThat(period).isNotEqualTo(period2);13 }14}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!