Please read Part I of this article series to get more context on this article.

After writing the first integration test, we saw the advantages of writing such a test, and we wanted to write more. We needed a strategy to identify when to write the integration tests, which classes to mock, and for which classes should we continue writing the unit tests.

Component Relationships

Here is the component diagram our typical REST endpoint (as described in part I).

We looked at the relationship between the component under test (CUT) and other components to determine which components should be mocked, which components…


We have a monolith and a few months ago our test pyramid looked like this:

We had a lot of unit tests, more end-to-end tests than we would have liked and zero integration tests. We fixed our test pyramid by writing more integration tests and by reducing the number of end-to-end tests. The pyramid is near perfect now.


A colleague of mine once said:

“The more I read about Optional, the more I like it.”

And another colleague of mine said:

“Optional initiative is overwhelming. You should give some space to us.”

So apparently, a small dose of Optional is easier to digest than one long tutorial. So here we go!

  1. Use Optional when a method returns null : Talks about different ways in which Optional can be used while calling a method that can potentially return a null value.
  2. Deprecating @Nonnull and @Nullable annotations : Shows why these annotations are no longer needed after the introduction of Optional in Java 8.

Java programmers tend to perform null checks when they are not always required. Performing null check on a method return value is one example. This tendency is a result of lack of support from Java compiler or Java runtime. That is, Java does not and cannot guarantee that a given reference is always a non-null value.

Java is trying to reduce the number of unnecessary null checks with the introduction of Optional. This article talks about different Optional APIs and how they can be used to handle the null values gracefully.

Using A Default Value

If a method returns null then use a fall…


If used correctly, Optional in Java makes @Nullable and @Nonnull annotations obsolete.

What Is Optional

Java 8 introduced a class called Optional. The intention is to solve the problem of null reference as a method parameter and as a returned value.

The recommendation is to use Optional.empty() instead of null. For example, a person may or may not have a title because of which the following method can return null:

public @Nullable String getTitle() {
return this.title;
}

With the introduction of Optional, this method should be written as:

public Optional<String> getTitle() {
return Optional.ofNullable(this.title);
}

The caller of this method knows that the returned value may or may not be present and can handle…

Viraj Turakhia

A software engineer with 17 years of experience and still learning.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store