githubEdit

πŸ—£οΈFluent Assertions

How to assert Result objects fluently

You can use fluent assertions for Result objects to enhance the readability and expressiveness of your unit tests. These assertions are based on AssertJarrow-up-right, an open-source Java library that offers a fluent API for writing assertions in test cases.

circle-info

AssertJarrow-up-right features a comprehensive and intuitive set of strongly-typed assertions for unit testing. It is a popular choice among Java developers due to its effective features and compatibility with various testing frameworks like JUnitarrow-up-right and TestNGarrow-up-right.

How to Use this Add-On

Add this Maven dependency to your build:

Group ID
Artifact ID
Latest Version

com.leakyabstractions

result-assertj

circle-check

Asserting Result Objects

You can use ResultAssertions::assertThatarrow-up-right in your tests to create fluent assertions for result objects.

import static com.leakyabstractions.result.assertj.ResultAssertions.assertThat;

@Test
void testAssertThat() {
  // Given
  final int zero = 0;
  // When
  final Result<Integer, String> result = success(zero);
  // Then
  assertThat(zero).isZero();
  assertThat(result).hasSuccess(zero);
}

If, for any reason, you cannot statically import assertThat, you can use ResultAssert::assertThatResultarrow-up-right instead.

Conclusion

We covered how to use fluent assertions for Results. This approach allows you to write clear and expressive tests, enhancing the maintainability of your unit tests while ensuring that Result objects behave as expected.

circle-check

Last updated

Was this helpful?