LogoLogo
GitHubFree book
  • 🏠Introduction
  • Using the Library
    • ðŸŒąGetting Started
      • Adding Result to Your Build
      • Creating Results
    • ðŸŠīBasic Usage
      • Checking Success or Failure
      • Unwrapping Values
      • Conditional Actions
    • 🚀Advanced Usage
      • Screening Results
      • Transforming Results
    • 🏁Recap
  • Add-ons
    • ðŸ’ĪLazy Results
    • ðŸ—ĢïļFluent Assertions
    • 📜Jackson Module
    • 🧑‍🚀Micronaut Serialization
  • Other resources
    • ðŸ“ĶBill of Materials
    • 📈Benchmarks
    • ðŸĪ–Demo Projects
      • Spring Boot Demo Project
      • Micronaut Demo Project
    • ⚖ïļLicense
Powered by GitBook
LogoLogo

Source Code

  • GitHub
  • License

Quality

  • SonarCloud
  • Benchmarks

Documentation

  • Free book
  • Javadoc

Releases

  • Maven Central
  • Bill of Materials

Copyright 2024 Guillermo Calvo

On this page
  • Checking Success
  • Checking Failure
  • Conclusion

Was this helpful?

Edit on GitHub
Export as PDF
  1. Using the Library
  2. Basic Usage

Checking Success or Failure

How to find out if the operation succeded or failed

PreviousBasic UsageNextUnwrapping Values

Last updated 8 months ago

Was this helpful?

As we discovered earlier, we can easily determine if a given Result instance is successful or not.

Checking Success

We can use to obtain a boolean value that represents whether a result is successful.

@Test
void testHasSuccess() {
  // Given
  Result<?, ?> result1 = success(1024);
  Result<?, ?> result2 = failure(1024);
  // When
  boolean result1HasSuccess = result1.hasSuccess();
  boolean result2HasSuccess = result2.hasSuccess();
  // Then
  assertTrue(result1HasSuccess);
  assertFalse(result2HasSuccess);
}

Checking Failure

We can also use to find out if a result contains a failure value.

@Test
void testHasFailure() {
  // Given
  Result<?, ?> result1 = success(512);
  Result<?, ?> result2 = failure(512);
  // When
  boolean result1HasFailure = result1.hasFailure();
  boolean result2HasFailure = result2.hasFailure();
  // Then
  assertFalse(result1HasFailure);
  assertTrue(result2HasFailure);
}

Conclusion

We discussed how to determine the state of a Result object using and . These methods provide a straightforward way to identify the outcome of an operation, helping you make decisions based on the outcome.

ðŸŠī
Result::hasSuccess
Result::hasFailure
hasSuccess
hasFailure