[TDD] Developing REST API using Test Driven Development — with Java, Spring Boot and JUnit

Prabhash
4 min readAug 29, 2021

In my last post, We got to know what it is and how to get started with TDD. Also we wrote a simple java program together as an example. You can check the post here : [TDD] Test Driven Development — with Java and JUnit

Test Driven Development is a discipline that we should master, and we all know that practice is the key to achieve that.

So, In this blog we are going one step further and we will write a sample REST API with one endpoint.

Problem Statement: Develop an application to expose students data using REST API.

Tech Stack: We will use Java and Spring Boot to develop this application, JUnit for writing test cases, and Gradle for dependency management. We will be writing code using IntelliJ IDEA, you can use your favorite IDE.

Scope: We will keep the scope limited, by not using database. Also this code is not production grade, and we can still refactor this (perhaps sometime later).

I will add the git repository url of the source code used in this demo at the end of this blog.

Now, Let’s start writing some code.

1]. Create a new Spring Boot project:

Go to https://start.spring.io and fill the project details details. We are using project name as ‘restapi-tdd’. Add ‘Spring-Web’ as dependency and Click on generate.

--

--

Prabhash

A Software Engineer who loves solving problems and watches tons of TV Series!