Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

When we’re testing methods that return Optional objects, we may need to write assertions that either check if the Optional has a value or check the value inside it.

In this short tutorial, we’ll look at how to write these assertions using functions from both JUnit and AssertJ.

2. Test Whether an Optional Is Empty or Not

If we only need to find out whether the Optional has a value, we can assert on isPresent or isEmpty.

2.1. Test Optional Has a Value

If an Optional has a value, we can assert on Optional.isPresent:

assertTrue(optional.isPresent());

However, the AssertJ library provides a more fluent way of expressing this:

assertThat(optional).isNotEmpty();

2.2. Test Optional Is Empty

We can reverse the logic when using JUnit:

assertFalse(optional.isPresent());

Also, in Java 11 onwards, we can use Optional.isEmpty:

assertTrue(optional.isEmpty());

However, AssertJ provides us a neat alternative too:

assertThat(optional).isEmpty();

3. Testing the Value of an Optional

Often we want to test the value inside an Optional rather than just presence or absence.

3.1. Using JUnit Assertions

We can use Optional.get to provide the value and then write an assertion on that:

Optional<String> optional = Optional.of("SOMEVALUE");
assertEquals("SOMEVALUE", optional.get());

However, using get may cause an exception, which makes it harder to understand a test failure. So, we may prefer to assert whether the value is present first:

assertTrue(optional.isPresent());
assertEquals("SOMEVALUE", optional.get());

However, Optional supports the equals method, so we can use an Optional with the correct value in it as part of a general equality assertion:

Optional<String> expected = Optional.of("SOMEVALUE");
Optional<String> actual = Optional.of("SOMEVALUE");
assertEquals(expected, actual);

3.2. Using AssertJ

With AssertJ, we can use the hasValue fluent assertion:

assertThat(Optional.of("SOMEVALUE")).hasValue("SOMEVALUE");

4. Conclusion

In this article, we looked at a few ways to test Optional.

We saw how the built-in JUnit assertions can be used with isPresent and get. We also saw how Optional.equals provides us a way to compare Optional objects in our assertions.

Finally, we looked at the AssertJ assertions, which give us fluent language to check our Optional values. 

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW