How to create effective unit testing with Java

Rafael Faita
Javarevisited
Published in
4 min readOct 28, 2020

--

Context

Creating automatic unit tests is one of the most common tasks today in development day by day, but to create an effective unit test you should cover some specific aspects of testing. In this article, I will show you some of these aspects.

Photo by Joshua Hoehne on Unsplash

First things first

Let’s define a common scenario of business needs, a simple and very common Order Service, where our service will receive an object with the name of the client, the items of order, and the delivery address.

The ordering service must validate if the name, items, and delivery address are not empty and validate if the amount of each item is greater than zero.

If all validation is passed, then we have to persist this information on the database.

Below, I show the service class and the validator class:

--

--