Bruno Krebs

@krebs.bruno

Spring Boot: REST + TDD from scratch

Disclaimer: Since there is a lot of stuff written about unit tests, in this article I will focus on how to write a RESTful application with integration tests and how to run them during the build process.

After spending a whole year away from Java development, I decided to go back and check how things were doing. And I can tell, I like it. Spring has always caught my attention due to their goal (and success) on making Java enterprise development as easy as possible. Spring Boot, which we will use in this article, is a project that does that with excellence. Using it to start our project's skeleton is easy, since they favor convention over configuration.

Upon Spring Boot, we can use RESTful services to create a client-agnostic application and make it bug-free along side TDD methodologies.

Setting up Spring Boot

First things first. Even though TDD claims that we write failing tests first, to make them pass after, we first need to make a bootable piece of software. Mainly because we need to be able to start a server to see our first test fail during the integration test phase of Maven.

To start an application with Spring Boot is really easy, we just need two files and one command. Let's start with our pom.xml file.

This file, which should be placed in the root directory of our application, defines our project as being a child of spring-boot-starter-parent and adds spring-boot-start-web as a dependency. This combination enable us to run a web application with a single and very small class. Check it out:

After creating this class, which should be placed at ./src/main/java/br/com/brunokrebs/ directory, we will be able to run it issuing the following command on our root directory: mvn spring-boot:run.

Of course, this application doesn't do much right now. Actually it just starts up and keep running a web server. But no RESTful endpoint, no web page, no nothing. So, since we are following TDD methodology, let's write a very simple integration test that expects our web application to answer to a GET request.

Integrating Spring Boot with Maven

To be able to get our tests run automatically in the integration-test phase of Maven, we have to make a few changes in our previously created pom.xml. These changes are small and simple. First, lets add, after spring-boot-starter-web, the following three dependencies:

Then we add maven-failsafe-plugin plugin to execute the integration tests that we are about to create.

And finally we add two execution tasks, to spring-boot-maven-plugin, to deploy our application in order to be able to respond to tests. Note that we already added this plugin in our pom.xml file before. So we just need to add the two tasks:

Now, to start our build process executing the integration tests, we issue the following command: mvn verify. This command will build our project, start a Tomcat container, deploy our project, and issue tests against it. Well, it would issue tests, we didn't write any yet.

Writing our first test

The last added plugin, maven-failsafe-plugin, considers, by default, any class ending with IT as an integration test that must be run. So lets add one and see our first test fail.

We must create this class, following Java and Maven conventions, in the ./src/test/java/br/com/brunokrebs/EchoIT.java file. After creating it, we can run 'mvn verify' command to see it fail.

Nice, we have our first failing test that is issued against our project. So, lets fix it. To make things easy, lets just make a few changes to our main class, AbtbvApplication.java:

As we can see above, we have made four changes to our class. First, we have added a new @RestController annotation to it, which marks it as a REST controller. Second, we have created an inner class, called Message, to hold the message being echoed. Third, we have added a new method that is annotated with @RequestMapping, which configures it to answer to GET requests at '/echo/{message}'. This method takes as a parameter a String message that is annotated with @PathVariable, which makes it automatically get any value passed in the place of {message}. And fourth, we made this method return a new instance of Message. This new instance gets serialized as JSON, just as expected by our test.

Done! We can now execute again 'mvn verify' and see our first test succeed.

Conclusion

As we can see in this article, creating an skeleton of a REST oriented enterprise application with Spring Boot is really easy. This skeleton doesn't do much, but, with the advent of integration tests that are run in the build process, makes it a very good foundation to create any production-ready, bug-free, application.

I hope you guys liked this article, as I'm looking forward to write a few more related to it. The following articles, which I didn't decided precisely how I'm going to structure, will add some nice features that are expected in an enterprise application. Example given: data sources, graphic user interfaces (probably starting with a web layer based on Angular) and so on.

My name is Bruno Krebs and you can reach me on my Twitter, my LinkedIn and my Github account. Also, feel free to add any comments here or in any of my articles.
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

More by Bruno Krebs

Topics of interest

More Related Stories