Class ResultAssert<S,F>

java.lang.Object
org.assertj.core.api.AbstractAssert<ResultAssert<S,F>,Result<S,F>>
com.leakyabstractions.result.assertj.ResultAssert<S,F>
Type Parameters:
S - type of the success value contained in the Result.
F - type of the failure value contained in the Result.
All Implemented Interfaces:
Assert<ResultAssert<S,F>,Result<S,F>>, Descriptable<ResultAssert<S,F>>, ExtensionPoints<ResultAssert<S,F>,Result<S,F>>

public class ResultAssert<S,F> extends AbstractAssert<ResultAssert<S,F>,Result<S,F>>
Assertions for Result.
Author:
Guillermo Calvo
  • Method Details

    • assertThatResult

      public static <S, F> ResultAssert<S,F> assertThatResult(Result<S,F> actual)
      Create assertion for Result.

      This static method is provided for convenience, in case ResultAssertions.assertThat(Result) can't be statically imported.

      Type Parameters:
      S - type of the success value contained in the Result.
      F - type of the failure value contained in the Result.
      Parameters:
      actual - the actual value.
      Returns:
      the created assertion object.
    • hasSuccess

      public ResultAssert<S,F> hasSuccess()
      Verifies that the actual Result is successful.

      Assertions will pass:

        
       assertThat(Results.success("yay")).hasSuccess();
       assertThat(Results.success(123)).hasSuccess();
       
       
      Assertions will fail:
        
       assertThat(Results.failure("nay")).hasSuccess();
       assertThat(Results.failure(123)).hasSuccess();
       
       
      Returns:
      this assertion object.
    • hasSuccess

      public ResultAssert<S,F> hasSuccess(S expectedValue)
      Verifies that the actual Result is a successful result containing the given value.

      Assertions will pass:

        
       assertThat(Results.success("yay")).hasSuccess("yay");
       assertThat(Results.success(1234)).hasSuccess(1234);
       
       
      Assertions will fail:
        
       assertThat(Results.success("yay")).hasSuccess("nay");
       assertThat(Results.failure("yay")).hasSuccess("yay");
       
       
      Parameters:
      expectedValue - the expected success value inside the Result.
      Returns:
      this assertion object.
    • hasSuccessSameAs

      public ResultAssert<S,F> hasSuccessSameAs(S expectedValue)
      Verifies that the actual Result is a successful result containing the instance given as an argument (i.e. it must be the same instance).

      Given:

        
       static {
           final String FOOBAR = "foobar";
       }
       
       
      Assertions will pass:
        
       assertThat(Results.success(FOOBAR)).hasSuccessSameAs(FOOBAR);
       assertThat(Results.success(10)).hasSuccessSameAs(10);
       
       
      Assertions will fail:
        
       assertThat(Results.success("yay")).hasSuccessSameAs("nay");
       assertThat(Results.failure(FOOBAR)).hasSuccessSameAs(FOOBAR);
       
       
      Parameters:
      expectedValue - the expected success value inside the Result.
      Returns:
      this assertion object.
    • hasSuccessSatisfying

      public ResultAssert<S,F> hasSuccessSatisfying(Consumer<S> requirement)
      Verifies that the actual Result is a successful result and invokes the given Consumer with the success value for further assertions.

      Should be used as a way of deeper asserting on the containing object, as further requirement(s) for the value.

      Assertions will pass:

        
       // one requirement
       assertThat(Results.success(10)).hasSuccessSatisfying(s -> {
           assertThat(s).isGreaterThan(9);
       });
       assertThat(Results.success("yay")).hasSuccessSatisfying(s -> {
           assertThat(s).isEqualTo("yay");
       });
      
       // multiple requirements
       assertThat(Results.success("hello")).hasSuccessSatisfying(s -> {
           assertThat(s).isEqualTo("hello");
           assertThat(s).startsWith("h");
           assertThat(s).endsWith("o");
       });
       
       
      Assertions will fail:
        
       assertThat(Results.success("yay")).hasSuccessSatisfying(s -> assertThat(s).isEqualTo("nay"));
       assertThat(Results.failure("yay")).hasSuccessSatisfying(s -> {});
       
       
      Parameters:
      requirement - to further assert on the success value held by the Result
      Returns:
      this assertion object.
    • hasSuccessSatisfying

      public ResultAssert<S,F> hasSuccessSatisfying(Condition<? super S> condition)
      Verifies that the actual Result is a successful result whose success value satisfies the given Condition.

      Given:

        
       static {
           final Condition<Integer> IS_NEGATIVE = new Condition<>(i -> i < 0, "a negative number");
       }
       
       
      Assertion will pass:
        
       assertThat(Results.success(-1)).hasSuccessSatisfying(IS_NEGATIVE);
       
       
      Assertions will fail:
        
       assertThat(Results.success(1234)).hasSuccessSatisfying(IS_NEGATIVE);
       assertThat(Results.failure(-123)).hasSuccessSatisfying(IS_NEGATIVE);
       
       
      Parameters:
      condition - the given condition
      Returns:
      this assertion object.
    • hasSuccessInstanceOf

      public ResultAssert<S,F> hasSuccessInstanceOf(Class<?> clazz)
      Verifies that the actual Result is a successful result containing a value that is an instance of the given class.

      Assertions will pass:

        
       assertThat(Results.success("hello"))
               .hasSuccessInstanceOf(String.class)
               .hasSuccessInstanceOf(Object.class);
       assertThat(Results.success(1234)).hasSuccessInstanceOf(Integer.class);
       
       
      Assertions will fail:
        
       assertThat(Results.success("hello")).hasSuccessInstanceOf(Integer.class);
       assertThat(Results.failure("hello")).hasSuccessInstanceOf(String.class);
       
       
      Parameters:
      clazz - the expected class of the success value inside the Result
      Returns:
      this assertion object.
    • hasSuccessThat

      public ObjectAssert<S> hasSuccessThat()
      Verifies that the actual Result is a successful result and returns an Object assertion that allows chaining (object) assertions on its success value.

      Assertions will pass:

        
       assertThat(Results.success(0)).hasSuccessThat().isZero();
       assertThat(Results.success(100)).hasSuccessThat().isGreaterThan(10);
       
       
      Assertions will fail:
        
       assertThat(Results.failure(0)).hasSuccessThat().isZero();
       assertThat(Results.success(1)).hasSuccessThat().isGreaterThan(10);
       
       
      Returns:
      a new ObjectAssert for assertions chaining on the success value.
      See Also:
    • hasSuccessThat

      public <T extends AbstractAssert<?, ?>> T hasSuccessThat(InstanceOfAssertFactory<?,T> assertFactory)
      Verifies that the actual Result is a successful result and returns a new assertion instance to chain assertions on its success value.

      The assertFactory parameter allows to specify an InstanceOfAssertFactory, which is used to get the assertions narrowed to the factory type.

      Wrapping the given InstanceOfAssertFactory with Assertions.as(InstanceOfAssertFactory) makes the assertion more readable.

      Assertions will pass:

        
       assertThat(Results.success(0)).hasSuccessThat(as(InstanceOfAssertFactories.INTEGER)).isZero();
       assertThat(Results.success("hello")).hasSuccessThat(as(InstanceOfAssertFactories.STRING)).startsWith("h");
       
       
      Assertions will fail:
        
       assertThat(Results.success("hello")).hasSuccessThat(as(InstanceOfAssertFactories.INTEGER)).isZero();
       assertThat(Results.failure("hello")).hasSuccessThat(as(InstanceOfAssertFactories.STRING)).startsWith("h");
       
       
      Type Parameters:
      T - the type of the resulting Assert
      Parameters:
      assertFactory - the factory which verifies the type and creates the new Assert
      Returns:
      a new narrowed ObjectAssertProxy instance for assertions chaining on the success value
    • hasFailure

      public ResultAssert<S,F> hasFailure()
      Verifies that the actual Result is failed.

      Assertions will pass:

        
       assertThat(Results.failure("yay")).hasFailure();
       assertThat(Results.failure(123)).hasFailure();
       
       
      Assertions will fail:
        
       assertThat(Results.success("nay")).hasFailure();
       assertThat(Results.success(123)).hasFailure();
       
       
      Returns:
      this assertion object.
    • hasFailure

      public ResultAssert<S,F> hasFailure(F expectedValue)
      Verifies that the actual Result is a failed result containing the given value.

      Assertions will pass:

        
       assertThat(Results.failure("yay")).hasFailure("yay");
       assertThat(Results.failure(1234)).hasFailure(1234);
       
       
      Assertions will fail:
        
       assertThat(Results.failure("yay")).hasFailure("nay");
       assertThat(Results.success("yay")).hasFailure("yay");
       
       
      Parameters:
      expectedValue - the expected failure value inside the Result.
      Returns:
      this assertion object.
    • hasFailureSameAs

      public ResultAssert<S,F> hasFailureSameAs(F expectedValue)
      Verifies that the actual Result is a failed result containing the instance given as an argument (i.e. it must be the same instance).

      Given:

        
       static {
           final String FOOBAR = "foobar";
       }
       
       
      Assertions will pass:
        
       assertThat(Results.failure(FOOBAR)).hasFailureSameAs(FOOBAR);
       assertThat(Results.failure(10)).hasFailureSameAs(10);
       
       
      Assertions will fail:
        
       // not equal:
       assertThat(Results.failure("yay")).hasFailureSameAs("nay");
       assertThat(Results.success(FOOBAR)).hasFailureSameAs(FOOBAR);
       
       
      Parameters:
      expectedValue - the expected failure value inside the Result.
      Returns:
      this assertion object.
    • hasFailureSatisfying

      public ResultAssert<S,F> hasFailureSatisfying(Consumer<F> requirement)
      Verifies that the actual Result is a failed result and invokes the given Consumer with the failure value for further assertions.

      Should be used as a way of deeper asserting on the containing object, as further requirement(s) for the value.

      Assertions will pass:

        
       // one requirement
       assertThat(Results.failure(10)).hasFailureSatisfying(f -> {
           assertThat(f).isGreaterThan(9);
       });
       assertThat(Results.failure("yay")).hasFailureSatisfying(f -> {
           assertThat(f).isEqualTo("yay");
       });
      
       // multiple requirements
       assertThat(Results.failure("hello")).hasFailureSatisfying(f -> {
           assertThat(f).isEqualTo("hello");
           assertThat(f).startsWith("h");
           assertThat(f).endsWith("o");
       });
       
       
      Assertions will fail:
        
       assertThat(Results.success("hello")).hasSuccessSatisfying(s -> assertThat(s).isEqualTo("hello"));
       assertThat(Results.failure("hello")).hasSuccessSatisfying(s -> {});
       
       
      Parameters:
      requirement - to further assert on the failure value held by the Result
      Returns:
      this assertion object.
    • hasFailureSatisfying

      public ResultAssert<S,F> hasFailureSatisfying(Condition<? super F> condition)
      Verifies that the actual Result is a failed result whose failure value satisfies the given Condition.

      Given:

        
       static {
           final Condition<Integer> IS_NEGATIVE = new Condition<>(i -> i < 0, "a negative number");
       }
       
       
      Assertion will pass:
        
       assertThat(Results.failure(-1)).hasFailureSatisfying(IS_NEGATIVE);
       
       
      Assertions will fail:
        
       assertThat(Results.failure(1234)).hasFailureSatisfying(IS_NEGATIVE);
       assertThat(Results.success(-123)).hasFailureSatisfying(IS_NEGATIVE);
       
       
      Parameters:
      condition - the given condition
      Returns:
      this assertion object.
    • hasFailureInstanceOf

      public ResultAssert<S,F> hasFailureInstanceOf(Class<?> clazz)
      Verifies that the actual Result is a failed result containing a value that is an instance of the given class.

      Assertions will pass:

        
       assertThat(Results.failure("hello"))
               .hasFailureInstanceOf(String.class)
               .hasFailureInstanceOf(Object.class);
       assertThat(Results.failure(123)).hasSuccessInstanceOf(Integer.class);
       
       
      Assertions will fail:
        
       assertThat(Results.failure("hello")).hasFailureInstanceOf(Integer.class);
       assertThat(Results.success("hello")).hasFailureInstanceOf(String.class);
       
       
      Parameters:
      clazz - the expected class of the failure value inside the Result
      Returns:
      this assertion object.
    • hasFailureThat

      public ObjectAssert<F> hasFailureThat()
      Verifies that the actual Result is a failed result and returns an Object assertion that allows chaining (object) assertions on its failure value.

      Assertions will pass:

        
       assertThat(Results.failure(10)).hasFailureThat().isZero();
       assertThat(Results.failure(100)).hasFailureThat().isGreaterThan(10);
       
       
      Assertions will fail:
        
       assertThat(Results.success(0)).hasFailureThat().isZero();
       assertThat(Results.failure(1)).hasFailureThat().isGreaterThan(10);
       
       
      Returns:
      a new ObjectAssert for assertions chaining on the success value.
      See Also:
    • hasFailureThat

      public <T extends AbstractAssert<?, ?>> T hasFailureThat(InstanceOfAssertFactory<?,T> assertFactory)
      Verifies that the actual Result is a failed result and returns a new assertion instance to chain assertions on its failure value.

      The assertFactory parameter allows to specify an InstanceOfAssertFactory, which is used to get the assertions narrowed to the factory type.

      Wrapping the given InstanceOfAssertFactory with Assertions.as(InstanceOfAssertFactory) makes the assertion more readable.

      Assertions will pass:

        
       assertThat(Results.failure(0)).hasFailureThat(as(InstanceOfAssertFactories.INTEGER)).isZero();
       assertThat(Results.failure("hello")).hasFailureThat(as(InstanceOfAssertFactories.STRING)).startsWith("h");
       
       
      Assertions will fail:
        
       assertThat(Results.failure("hello")).hasFailureThat(as(InstanceOfAssertFactories.INTEGER)).isZero();
       assertThat(Results.success("hello")).hasFailureThat(as(InstanceOfAssertFactories.STRING)).startsWith("h");
       
       
      Type Parameters:
      T - the type of the resulting Assert
      Parameters:
      assertFactory - the factory which verifies the type and creates the new Assert
      Returns:
      a new narrowed ObjectAssertProxy instance for assertions chaining on the success value