How to use shouldHaveDays method of org.assertj.core.error.ShouldHavePeriod class

Best Assertj code snippet using org.assertj.core.error.ShouldHavePeriod.shouldHaveDays

Source:AbstractPeriodAssert.java Github

copy

Full Screen

...12 */13package org.assertj.core.api;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 value70 * @return this assertion object71 * @throws AssertionError if the actual {@code Period} is {@code null}72 * @throws AssertionError if the actual {@code Period} does not have the given months73 * @since 3.17.074 */75 public SELF hasMonths(int expectedMonths) {76 isNotNull();77 int actualMonths = actual.getMonths();78 if (expectedMonths != actualMonths) {79 throw Failures.instance().failure(info, shouldHaveMonths(actual, actualMonths, expectedMonths), actualMonths,80 expectedMonths);81 }82 return myself;83 }84 /**85 * Verifies that the actual {@code Period} has the given days.86 * <p>87 * Example :88 * <pre><code class='java'> // assertion will pass89 * assertThat(Period.ofDays(5)).hasDays(5);90 *91 * // assertion will fail92 * assertThat(Period.ofDays(5)).hasDays(1);</code></pre>93 *94 * @param expectedDays the expected days value95 * @return this assertion object96 * @throws AssertionError if the actual {@code Period} is {@code null}97 * @throws AssertionError if the actual {@code Period} does not have the given days98 * @since 3.17.099 */100 public SELF hasDays(int expectedDays) {101 isNotNull();102 int actualDays = actual.getDays();103 if (expectedDays != actualDays) {104 throw Failures.instance().failure(info, shouldHaveDays(actual, actualDays, expectedDays), actualDays, expectedDays);105 }106 return myself;107 }108 /**109 * Verifies that the actual {@code Period} is positive (i.e. is greater than {@link Period#ZERO}).110 * <p>111 * Example :112 * <pre><code class='java'> // assertion will pass113 * assertThat(Period.ofMonths(5)).isPositive();114 *115 * // assertion will fail116 * assertThat(Period.ofMonths(-2)).isPositive();</code></pre>117 * @return this assertion object118 * @throws AssertionError if the actual {@code Period} is {@code null}...

Full Screen

Full Screen

Source:ShouldHavePeriod_create_test.java Github

copy

Full Screen

...12 */13package org.assertj.core.error;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 }69 @Test70 void should_create_error_message_for_1_day() {71 // GIVEN72 int actualDays = 2;73 int expectedDays = 1;74 Period period = Period.ofDays(actualDays);75 // WHEN76 String errorMessage = shouldHaveDays(period, actualDays, expectedDays).create();77 // THEN78 then(errorMessage).isEqualTo(format("%nExpecting Period:%n P2D%nto have 1 day but had 2"));79 }80 @Test81 void should_create_error_message_for_negative_1_days() {82 // GIVEN83 int actualDays = 1;84 int expectedDays = -1;85 Period period = Period.ofDays(actualDays);86 // WHEN87 String errorMessage = shouldHaveDays(period, actualDays, expectedDays).create();88 // THEN89 then(errorMessage).isEqualTo(format("%nExpecting Period:%n P1D%nto have -1 day but had 1"));90 }91 @Test92 void should_create_error_message_for_months() {93 // GIVEN94 int actualMonths = 1;95 int expectedMonths = 2;96 Period period = Period.ofMonths(actualMonths);97 // WHEN98 String errorMessage = shouldHaveMonths(period, actualMonths, expectedMonths).create();99 // THEN100 then(errorMessage).isEqualTo(format("%nExpecting Period:%n P1M%nto have 2 months but had 1"));101 }...

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;3import static org.assertj.core.util.FailureMessages.actualIsNull;4import java.time.Period;5import org.assertj.core.api.AssertionInfo;6import org.assertj.core.internal.Periods;7import org.assertj.core.internal.StandardComparisonStrategy;8import org.junit.jupiter.api.Test;9class PeriodsTest {10 private final Periods periods = Periods.instance();11 void should_pass_if_period_has_expected_days() {12 periods.assertIsDaysEqual(info, Period.ofDays(2), 2);13 }14 void should_fail_if_period_is_null() {15 assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> periods.assertIsDaysEqual(someInfo(), null, 2))16 .withMessage(actualIsNull());17 }18 void should_fail_if_period_days_are_not_equal() {19 assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> periods.assertIsDaysEqual(someInfo(), Period.ofDays(2), 1))20 .withMessage(shouldHaveDays(Period.ofDays(2), 2, 1).create());21 }22 private static AssertionInfo someInfo() {23 return someInfo(StandardComparisonStrategy.instance());24 }25}

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.error.ShouldHavePeriod;2import org.assertj.core.error.ErrorMessageFactory;3import org.assertj.core.error.BasicErrorMessageFactory;4import org.junit.Test;5import static org.assertj.core.api.Assertions.assertThat;6import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;7import static org.assertj.core.util.FailureMessages.actualIsNull;8import java.time.Period;9import java.time.temporal.ChronoUnit;10public class AssertJTest {11 public void test() {12 Period period = Period.of(1, 2, 3);13 assertThat(period).hasDays(3);14 }15}16import org.assertj.core.error.ShouldHavePeriod;17import org.assertj.core.error.ErrorMessageFactory;18import org.assertj.core.error.BasicErrorMessageFactory;19import org.junit.Test;20import static org.assertj.core.api.Assertions.assertThat;21import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;22import static org.assertj.core.util.FailureMessages.actualIsNull;23import java.time.Period;24import java.time.temporal.ChronoUnit;25public class AssertJTest {26 public void test() {27 Period period = Period.of(1, 2, 3);28 assertThat(period).hasDays(2);29 }30}31import org.assertj.core.error.ShouldHavePeriod;32import org.assertj.core.error.ErrorMessageFactory;33import org.assertj.core.error.BasicErrorMessageFactory;34import org.junit.Test;35import static org.assertj.core.api.Assertions.assertThat;36import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;37import static org.assertj.core.util.FailureMessages.actualIsNull;38import java.time.Period;39import java.time.temporal.ChronoUnit;40public class AssertJTest {41 public void test() {42 Period period = Period.of(1, 2, 3);43 assertThat(period

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.error;2import org.assertj.core.description.Description;3import org.assertj.core.description.TextDescription;4import org.assertj.core.presentation.StandardRepresentation;5public class ShouldHavePeriod_create_Test {6 public void should_create_error_message() {7 Description description = new TextDescription("Test");8 String errorMessage = ShouldHavePeriod.shouldHaveDays(description, 5, 6).create(new StandardRepresentation());9 assertThat(errorMessage).isEqualTo(String.format("[Test] %n" +10 "but was not."));11 }12}13package org.assertj.core.error;14import org.assertj.core.api.Assertions;15import org.assertj.core.api.ThrowableAssert.ThrowingCallable;16import org.assertj.core.description.TextDescription;17import org.junit.Test;18public class ShouldHavePeriod_create_Test {19 public void should_create_error_message() {20 TextDescription description = new TextDescription("Test");21 ThrowingCallable code = () -> Assertions.assertThat(5L).as(description).isEqualTo(6L);22 Assertions.assertThatThrownBy(code).hasMessage(String.format("[Test] %n" +23 "but was not."));24 }25}26package org.assertj.core.error;27import org.assertj.core.api.Assertions;28import org.assertj.core.api.ThrowableAssert.ThrowingCallable;29import org.assertj.core.description.TextDescription;30import org.junit.Test;31public class ShouldHavePeriod_create_Test {32 public void should_create_error_message() {33 TextDescription description = new TextDescription("Test");34 ThrowingCallable code = () -> Assertions.assertThat(5L).as(description).isEqualTo(6L);35 Assertions.assertThatExceptionOfType(AssertionError.class).isThrownBy(code)36 .withMessage(String.format("[Test] %n" +

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1ShouldHavePeriod shouldHavePeriod = ShouldHavePeriod.shouldHaveDays(actual, expected);2ShouldHavePeriod_create_Test shouldHavePeriod_create_Test = new ShouldHavePeriod_create_Test();3AssertionError assertionError = shouldHavePeriod_create_Test.create(shouldHavePeriod);4fail(assertionError.getMessage());5}6}

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import java.time.Period;3public class PeriodAssertJTest {4 public static void main(String[] args) {5 Period period = Period.ofDays(2);6 assertThat(period).shouldHaveDays(2);7 }8}

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1public void test1() {2 assertThatThrownBy(() -> assertThat(Period.ofDays(1)).shouldHaveDays(1)).isInstanceOf(AssertionError.class)3 .hasMessageContaining("Expecting")4 .hasMessageContaining("to have days")5 .hasMessageContaining("but had")6 .hasMessageContaining("1");7}8public void test2() {9 assertThatThrownBy(() -> assertThat(Period.ofDays(1)).shouldHaveDays(1)).isInstanceOf(AssertionError.class)10 .hasMessageContaining("Expecting")11 .hasMessageContaining("to have days")12 .hasMessageContaining("but had")13 .hasMessageContaining("1");14}15public void test3() {16 assertThatThrownBy(() -> assertThat(Period.ofDays(1)).shouldHaveDays(1)).isInstanceOf(AssertionError.class)17 .hasMessageContaining("Expecting")18 .hasMessageContaining("to have days")19 .hasMessageContaining("but had")20 .hasMessageContaining("1");21}22public void test4() {23 assertThatThrownBy(() -> assertThat(Period.ofDays(1)).shouldHaveDays(1)).isInstanceOf(AssertionError.class)24 .hasMessageContaining("Expecting")25 .hasMessageContaining("to have days")26 .hasMessageContaining("but had")27 .hasMessageContaining("1");28}29public void test5() {30 assertThatThrownBy(() -> assertThat(Period.ofDays(1)).shouldHaveDays(1)).isInstanceOf(AssertionError.class)31 .hasMessageContaining("Expecting")32 .hasMessageContaining("to have days")33 .hasMessageContaining("but had")34 .hasMessageContaining("1");35}36public void test6() {37 assertThatThrownBy(() -> assertThat(Period.ofDays(1)).shouldHaveDays(1)).isInstanceOf(AssertionError.class)

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.error;2import org.assertj.core.api.Condition;3import org.assertj.core.api.TestCondition;4import org.assertj.core.internal.TestDescription;5import org.assertj.core.presentation.StandardRepresentation;6import org.junit.jupiter.api.Test;7import java.time.Period;8import static org.assertj.core.api.Assertions.assertThat;9import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;10public class ShouldHavePeriod_create_Test {11 public void should_create_error_message_for_period() {12 Condition<Period> condition = new TestCondition<>();13 String errorMessage = shouldHaveDays(condition, 1).create(new TestDescription("TEST"), new StandardRepresentation());14 assertThat(errorMessage).isEqualTo(String.format("[TEST] %n" +15 " <0>"));16 }17}18package org.assertj.core.error;19import org.assertj.core.api.Condition;20import org.assertj.core.api.TestCondition;21import org.assertj.core.internal.TestDescription;22import org.assertj.core.presentation.StandardRepresentation;23import org.junit.jupiter.api.Test;24import java.time.Period;25import static org.assertj.core.api.Assertions.assertThat;26import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;27public class ShouldHavePeriod_create_Test {28 public void should_create_error_message_for_period() {29 Condition<Period> condition = new TestCondition<>();30 String errorMessage = shouldHaveDays(condition, 1).create(new TestDescription("TEST"), new StandardRepresentation());31 assertThat(errorMessage).isEqualTo(String.format("[TEST] %n" +32 " <0>"));33 }34}35package org.assertj.core.error;36import org.assertj.core.api.Condition;37import org.assertj.core.api.TestCondition;38import org.assertj.core.internal.TestDescription;39import org.assertj.core.presentation.StandardRepresentation;40import org

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1public class ShouldHavePeriod_shouldHaveDays_Test {2 public void should_create_error_message() {3 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());4 then(message).isEqualTo(format("[Test] %n" +5 "to have 3 days but had 1"));6 }7}8public class ShouldHavePeriod_shouldHaveDays_Test {9 public void should_create_error_message() {10 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());11 then(message).isEqualTo(format("[Test] %n" +12 "to have 3 days but had 1"));13 }14}15public class ShouldHavePeriod_shouldHaveDays_Test {16 public void should_create_error_message() {17 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());18 then(message).isEqualTo(format("[Test] %n" +19 "to have 3 days but had 1"));20 }21}22public class ShouldHavePeriod_shouldHaveDays_Test {23 public void should_create_error_message() {24 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());25 then(message).isEqualTo(format("[Test] %n" +

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.error;2import org.assertj.core.api.Condition;3import org.assertj.core.api.TestCondition;4import org.assertj.core.internal.TestDescription;5import org.assertj.core.presentation.StandardRepresentation;6import org.junit.jupiter.api.Test;7import java.time.Period;8import static org.assertj.core.api.Assertions.assertThat;9import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;10public class ShouldHavePeriod_create_Test {11 public void should_create_error_message_for_period() {12 Condition<Period> condition = new TestCondition<>();13 String errorMessage = shouldHaveDays(condition, 1).create(new TestDescription("TEST"), new StandardRepresentation());14 assertThat(errorMessage).isEqualTo(String.format("[TEST] %n" +15 " <0>"));16 }17}18package org.assertj.core.error;19import org.assertj.core.api.Condition;20import org.assertj.core.api.TestCondition;21import org.assertj.core.internal.TestDescription;22import org.assertj.core.presentation.StandardRepresentation;23import org.junit.jupiter.api.Test;24import java.time.Period;25import static org.assertj.core.api.Assertions.assertThat;26import static org.assertj.core.error.ShouldHavePeriod.shouldHaveDays;27public class ShouldHavePeriod_create_Test {28 public void should_create_error_message_for_period() {29 Condition<Period> condition = new TestCondition<>();30 String errorMessage = shouldHaveDays(condition, 1).create(new TestDescription("TEST"), new StandardRepresentation());31 assertThat(errorMessage).isEqualTo(String.format("[TEST] %n" +32 " <0>"));33 }34}35package org.assertj.core.error;36import org.assertj.core.api.Condition;37import org.assertj.core.api.TestCondition;38import org.assertj.core.internal.TestDescription;39import org.assertj.core.presentation.StandardRepresentation;40import org

Full Screen

Full Screen

shouldHaveDays

Using AI Code Generation

copy

Full Screen

1public class ShouldHavePeriod_shouldHaveDays_Test {2 public void should_create_error_message() {3 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());4 then(message).isEqualTo(format("[Test] %n" +5 "to have 3 days but had 1"));6 }7}8public class ShouldHavePeriod_shouldHaveDays_Test {9 public void should_create_error_message() {10 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());11 then(message).isEqualTo(format("[Test] %n" +12 "to have 3 days but had 1"));13 }14}15public class ShouldHavePeriod_shouldHaveDays_Test {16 public void should_create_error_message() {17 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());18 then(message).isEqualTo(format("[Test] %n" +19 "to have 3 days but had 1"));20 }21}22public class ShouldHavePeriod_shouldHaveDays_Test {23 public void should_create_error_message() {24 String message = shouldHaveDays(3, 1).create(new TextDescription("Test"), new StandardRepresentation());25 then(message).isEqualTo(format("[Test] %n" +

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.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful