4. Spring WebFlux: Unit testing your REST API (Continuation)

George Gatheca
3 min readSep 15, 2021

--

On the previous article, we improved our application by adding database support to it. On this tutorial we will further improve on that by doing unit tests to the application.

Full code for this tutorial can be found on GitHub branch “4-unit-testing” by visiting this link. https://github.com/gathecageorge/spring-webflux-rest-api-intro/tree/4-unit-testing

Alternatively you can get code of previous tutorial and code along in this tutorial from the “3-using-relational-database” branch here https://github.com/gathecageorge/spring-webflux-rest-api-intro/tree/3-using-relational-database

INTRODUCTION

For this course we are going to use mockito and JUnit5 to do some testing for the StudentsController.java class. We will not go ahead to test every object or class in the application as its supposed to be done but concentrate on reactive part of the application only which is on the controller.

Other parts like Models, DTOs and SampleController.java class can be tested the same way using JUnit5.

To start create a class StudentsControllerTest.java on “test” workspace. For those using IntelliJ IDE, one can just use shortcut to create a skeleton of the test class by going to StudentsController.java file, and right click on class name, then choose generate Test. Select all the methods and also add a setup method.

Setup Method

The setup method will create an instance of controller for before running each test. We need 2 parameters when creating the controller which we mock using Mockito. We will then mock each action on a repository as required by the test methods.

1. GetStudentTest

To test this method, we need to call controller getStudent() method and pass a value like 1.

  • We then need to check positive case that the result is okay and no error response.
  • We then need to test for negative case, i.e. pass a value not contained in the database and also test that expected error or message is returned.

For this tests we need don’t need to connect to a real database which the repository uses to simulate a correct value and a not found value. For that we mock the repository findById() method and return a record when 1 is passed and empty when 2 is passed.

2. GetStudentsTest

To test this we use same concept as above. This time response expected is not of type Mono but Flux since we can get many. I have indicated using inline comments the code as necessary.

3. AddStudentTest

For this we introduce another concept which is simulate adding record to database and then set the DB id returned on an object then we can verify save was okay by checking object now has an ID.

4. UpdateStudentTest

For this we introduce another concept of verifying that data has really changed. You can follow from inline comments in code.

5. DeleteStudentTest

As a learning lesson, we have intentionally left the test for this method and its up to you to implement. I would be happy to assist in case of any problem by reaching me on my profile or via LinkedIn here. I wish you happy learning and making small improvements to how you code.

CONCLUSION

Well that was a lot of things in one lesson? What have we learnt?

  • We have learnt to use mockito to mock classes and actions that would otherwise require using a database and making unit tests run faster
  • We have learnt how to assert and verify output of our methods using StepVerifier
  • We have learnt of how to have fun while coding. Its not usually as hard as it seems.

WHATS NEXT

So what’s next after this? Well checkout for the next articles on the series.

  1. Spring WebFlux: Reactive micro service Introduction
  2. Spring WebFlux: Reactive micro service Practical guide Intro
  3. Spring WebFlux: Using Relational Database (MySQL & PostgreSQL)
  4. Spring WebFlux: Unit testing your REST API (Continuation) (You are here)
  5. Spring WebFlux: Tips & Tricks, Common mistakes
  6. Spring WebFlux: Load testing using JMeter

Want to learn more, have a topic in JAVA you might need articles on or have questions? Reach out to me on my profile or via LinkedIn here.

Hello👋, Did you find this tutorial helpful, please click the clap 👏 button below to show your support for the author 👇. Also this will help the algorithm for others who are learning to see this story.

--

--

George Gatheca

Senior Software Engineer; Spring Java enthusiast; Building scalable solutions; Reducing compute waste.