Top Description Fields Constructors Methods
org.assertj.core.api

public abstract Class AbstractDurationAssert<SELF extends AbstractDurationAssert<SELF>>

extends AbstractComparableAssert<SELF extends AbstractDurationAssert<SELF>, Duration>
Class Inheritance
Known Direct Subclasses
org.assertj.core.api.DurationAssert
Static Imports
org.assertj.core.error.ShouldBeCloseTo.shouldBeCloseTo, .ShouldHaveDuration.shouldHaveDays, .ShouldHaveDuration.shouldHaveHours, .ShouldHaveDuration.shouldHaveMillis, .ShouldHaveDuration.shouldHaveMinutes, .ShouldHaveDuration.shouldHaveNanos, .ShouldHaveDuration.shouldHaveSeconds, org.assertj.core.util.Preconditions.checkArgument

Assertions for Duration type.
Author
Filip Hrisafov
Since
3.15.0

Field Summary

Inherited from org.assertj.core.api.AbstractComparableAssert:
comparables

Constructor Summary

AccessConstructor and Description
protected
AbstractDurationAssert(Duration
the actual value to verify
duration
,
Class<?>
the "self type"
selfType
)

Creates a new org.assertj.core.api.AbstractDurationAssert

Method Summary

Modifier and TypeMethod and Description
private static Duration
absDiff(Duration actual, Duration expected)

public SELF

Returns:

this assertion object
hasDays
(long
the expected days value
otherDays
)

Verifies that the actual Duration has the given days.

public SELF

Returns:

this assertion object
hasHours
(long
the expected hours value
otherHours
)

Verifies that the actual Duration has the given hours.

public SELF

Returns:

this assertion object
hasMillis
(long
the expected milliseconds value
otherMillis
)

Verifies that the actual Duration has the given millis.

public SELF

Returns:

this assertion object
hasMinutes
(long
the expected minutes value
otherMinutes
)

Verifies that the actual Duration has the given minutes.

public SELF

Returns:

this assertion object
hasNanos
(long
the expected nanoseconds value
otherNanos
)

Verifies that the actual Duration has the given nanos.

public SELF

Returns:

this assertion object
hasSeconds
(long
the expected seconds value
otherSeconds
)

Verifies that the actual Duration has the given seconds.

public SELF

Returns:

this assertion object.
isCloseTo
(Duration
the given Duration to compare to actual.
expected
,
Duration
a positive Duration to express the maximum allowed difference.
allowedDifference
)

Verifies that the actual Duration is close to the given one within the given allowed difference (assertion succeeds if difference = allowed difference).

public SELF

Returns:

this assertion object
isNegative
()

Verifies that the actual Duration is negative (i.e. is less than Duration#ZERO).

public SELF

Returns:

this assertion object
isPositive
()

Verifies that the actual Duration is positive (i.e. is greater than Duration#ZERO).

public SELF

Returns:

this assertion object
isZero
()

Verifies that the actual Duration is equal to Duration#ZERO.

Inherited from org.assertj.core.api.AbstractComparableAssert:
inBinaryinHexadecimalisBetweenisEqualByComparingToisGreaterThanisGreaterThanOrEqualToisLessThanisLessThanOrEqualToisNotEqualByComparingToisStrictlyBetweenusingComparatorusingComparatorusingDefaultComparator

Constructor Detail

AbstractDurationAssertback to summary
protected AbstractDurationAssert(Duration duration, Class<?> selfType)

Creates a new org.assertj.core.api.AbstractDurationAssert

Parameters
duration:Duration

the actual value to verify

selfType:Class<?>

the "self type"

Method Detail

absDiffback to summary
private static Duration absDiff(Duration actual, Duration expected)
hasDaysback to summary
public SELF hasDays(long otherDays)

Verifies that the actual Duration has the given days.

Example :

 // assertion will pass
assertThat(Duration.ofDays(5)).hasDays(5);

// assertion will fail
assertThat(Duration.ofDays(5)).hasDays(1);
Parameters
otherDays:long

the expected days value

Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration does not have the given days
Since
3.15.0
hasHoursback to summary
public SELF hasHours(long otherHours)

Verifies that the actual Duration has the given hours.

Example :

 // assertion will pass
assertThat(Duration.ofHours(15)).hasHours(15);

// assertion will fail
assertThat(Duration.ofHours(15)).hasHours(5);
Parameters
otherHours:long

the expected hours value

Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration does not have the given hours
Since
3.15.0
hasMillisback to summary
public SELF hasMillis(long otherMillis)

Verifies that the actual Duration has the given millis.

Example :

 // assertion will pass
assertThat(Duration.ofMillis(250)).hasMillis(250);

// assertion will fail
assertThat(Duration.ofMillis(250)).hasMillis(700);
Parameters
otherMillis:long

the expected milliseconds value

Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration does not have the given millis
Since
3.15.0
hasMinutesback to summary
public SELF hasMinutes(long otherMinutes)

Verifies that the actual Duration has the given minutes.

Example :

 // assertion will pass
assertThat(Duration.ofMinutes(65)).hasMinutes(65);

// assertion will fail
assertThat(Duration.ofMinutes(65)).hasMinutes(25);
Parameters
otherMinutes:long

the expected minutes value

Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration does not have the given minutes
Since
3.15.0
hasNanosback to summary
public SELF hasNanos(long otherNanos)

Verifies that the actual Duration has the given nanos.

Example :

 // assertion will pass
assertThat(Duration.ofNanos(145)).hasNanos(145);

// assertion will fail
assertThat(Duration.ofNanos(145)).hasNanos(50);
Parameters
otherNanos:long

the expected nanoseconds value

Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration does not have the given nanos
Since
3.15.0
hasSecondsback to summary
public SELF hasSeconds(long otherSeconds)

Verifies that the actual Duration has the given seconds.

Example :

 // assertion will pass
assertThat(Duration.ofSeconds(250)).hasSeconds(250);

// assertion will fail
assertThat(Duration.ofSeconds(250)).hasSeconds(700);
Parameters
otherSeconds:long

the expected seconds value

Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration does not have the given seconds
Since
3.15.0
isCloseToback to summary
public SELF isCloseTo(Duration expected, Duration allowedDifference)

Verifies that the actual Duration is close to the given one within the given allowed difference (assertion succeeds if difference = allowed difference).

This is equivalent of: abs(actual - expected) <= allowed difference.

For readability you can use Assertions#withMarginOf(Duration) to express the allowed difference.

Examples:

 Duration twoMinutes = Duration.ofMinutes(2);
// assertions succeed:
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(3), Duration.ofMinutes(5));
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(-3), Duration.ofMinutes(10));

// assertion succeeds when difference is exactly equals to the allowed difference
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(3), Duration.ofMinutes(1));
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(-3), Duration.ofMinutes(5));

// assertions using within syntactic sugar
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(3), withMarginOf(Duration.ofMinutes(5)));
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(3), withMarginOf(Duration.ofMinutes(1)));

// assertions fail
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(5), withMarginOf(Duration.ofMinutes(1)));
assertThat(twoMinutes).isCloseTo(Duration.ofMinutes(-3), withMarginOf(Duration.ofMinutes(4)));
Parameters
expected:Duration

the given Duration to compare to actual.

allowedDifference:Duration

a positive Duration to express the maximum allowed difference.

Returns:SELF

this assertion object.

Exceptions
IllegalArgumentException:
  • if the expected Duration is null.
  • if the allowed difference Duration is null or negative.
AssertionError:
if the actual value is not close enough to the given one.
Since
3.18.0
isNegativeback to summary
public SELF isNegative()

Verifies that the actual Duration is negative (i.e. is less than Duration#ZERO).

Example :

 // assertion will pass
assertThat(Duration.ofMinutes(-15)).isNegative();

// assertion will fail
assertThat(Duration.ofMinutes(10)).isNegative();
Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration is not less than Duration#ZERO
Since
3.15.0
isPositiveback to summary
public SELF isPositive()

Verifies that the actual Duration is positive (i.e. is greater than Duration#ZERO).

Example :

 // assertion will pass
assertThat(Duration.ofHours(5)).isPositive();

// assertion will fail
assertThat(Duration.ofHours(-2)).isPositive();
Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration is not greater than Duration#ZERO
Since
3.15.0
isZeroback to summary
public SELF isZero()

Verifies that the actual Duration is equal to Duration#ZERO.

Example :

 // assertion will pass
assertThat(Duration.ZERO).isZero();

// assertion will fail
assertThat(Duration.ofMinutes(3)).isZero();
Returns:SELF

this assertion object

Exceptions
AssertionError:
  • if the actual Duration is null
  • if the actual Duration is not Duration#ZERO
Since
3.15.0