Spring Boot + RESTful + JUnit + Mockito + Hamcrest + EclEmma

In this tutorial I will show you how to setup from scratch a RESTful Spring Boot project and the unit test implementation using the following tech stack:

  • Spring Boot(1.4.2)
  • Maven (3.3.9)
  • Eclipse (Neon, 4.6.0)
  • Java 8
  • Packaging (JAR)
  • JPA
  • RESTful
  • H2
  • JUnit
  • Mockito
  • Hamcrest
  • EclEmma (Coverage Unit Testing Tool)

Project Creation

First we will use a very useful Spring initializer in order to create our maven project with the dependencies listed above.

  1. Go to → https://start.spring.io/
  2. Leave everything as it is and select the following dependencies: Web, JPA, and H2.

Click on Generate Project button and it will download a zip file (demo.zip) with our maven project.

Import Project into Eclipse

  1. Unzip the zip file.
  2. Import into Eclipse as “Existing Maven Project
  3. Choose the root directory of the project generated (where the pom.xml file is located) and click on Finish.

It will display the next project structure.

pom.xml file

Model Creation

Let´s create our model class called ToDo(Entity class).

Data Layer (JPA Repository)

Now we are going to create our JPA repository using the ToDo class.

Service Layer

Now let´s create our service layer(interface and implementation). We will inject the repository into the service.

Interface

Implementation

Controller Layer

Now it´s time to create our controller layer, we will inject our service into the RestController layer.

Exception Handler

Now we need to implement our Exception Handler with the following classes:

  • ToDoException.java
  • ErrorResponse.java
  • RestExceptionHandler.java

Initialize Sample Data

Now we need to initialize our database (H2) with sample data using the interface CommandLineRunner. This interface is used to indicate that a bean should run when it is contained within a SpringApplication.

Note: out-of-the-box Spring Boot will setup the database (H2) configurations.

Launching the Application from Eclipse

Right click on DemoApplication.java → Run As → Java Application

Testing RESTful calls

I recommend use cURL (command line tool) to generate the http requests, but in this tutorial we will use a more friendly tool called Postman. In order to use this tool please open the next link and install it in your browser.

Get All ToDo’s

GET Request → http://localhost:8080/todo/

Get ToDo by Id

GET Request → http://localhost:8080/todo/4

Create ToDo

POST Request → http://localhost:8080/todo/

Header → Content-Type : application/json

Body → {“text” : “ToDo Sample”, “completed” : “false” }

Output

Update ToDo

PATCH Request → http://localhost:8080/todo/

Header → Content-Type : application/json

Body → {“id”: “5”, “text” : “ToDo Sample Updated”, “completed” : “true” }

Output

Remove ToDo

DELETE Request → http://localhost:8080/todo/5

Output

Invalid Requests

GET Request for invalid id → http://localhost:8080/todo/0

Output

GET Request for invalid argument→ http://localhost:8080/todo/f

Output

Note: if you got a “Bad String” message in the body request, please replace the double quotes manually (format issue for copy/paste).

Double quotes syntax error
Double quites expected syntax

Unit Testing

Now that we have validated the expected behavior of our RESTfull calls, finally we will implement the unit test logic into the Controller and Service layer.

ToDoControllerTest (Controller Layer)

  • JUnit
  • Hamcrest
  • MockMvc

ToDoServiceTest (Service Layer)

  • JUnit
  • Mockito

Coverage

In order to know and have more control about the unit testing coverage we are going to use a very useful Eclipse plugin called EclEmma.

  1. In Eclipse go to “Help” menu
  2. Open Eclipse Market
  3. Search for “EclEmma”
  4. Install it

5. Right click on the project

6. Coverage As → JUnit Test

JUnit Output

Coverage Output

Class Level Output

That´s all folks!!! I hope you can find this post useful. Any comment or feedback please share in the responses section :)

Git Repository

Here the repository in case you want to clone the code.