Reactive Microservices(Part1)- Introduction to Springboot, Spring WebFlux and Spoc Test Frameworks with Simple RESTful API.

Suresh Babu G
5 min readMay 18, 2022

Microservices are rapidly growing in the present IT world, so we need to be proficient in Designing, Developing, Unit Testing, Integration Testing, Volume and Performance Testing, CI/CD and Operational maintenance etc. are so important in building Enterprise level applications.

As a Microservices Developer, performing each phase of these development activities are very important and need to maintain a clean and advanced coding standards, I have started articulating these all parts in an easy way and I would be covering all important key parts for developing microservices with latest tech stack and advanced ways of building and deployment approaches in ongoing articles, hope you will find them useful and enjoy reading and encourage me with your feedback. thank you..!!

Let’s get started with a simple ‘Hello World!’ Microservice endpoint and I will explain all the steps involved in a detailed way.

Simple Requirement

Create a simple microservice endpoint which will accept GET protocol and return ‘Hello World!’.

Pre requisite

Java, Spring Reactive, Gradle, Spoc, Git, IntelliJ IDE etc…

Technology Stack used

  • Programming: Java18
  • Frameworks: Springboot (Core framework), SpringWebFlux (Reactive programming), Spoc (Testing framework)
  • Build Tools: Gradle
  • Utils: Lombok (For avoiding Boilerplate code)
  • IDE: IntelliJ

General Microservices Component Diagram

General Microservices Component Diagram

Development

Note: For better inline understanding, I am adding all technical details in the code sections as a comments or Doc comment definitions wherever needed.

Step1: Create a Springbooot project skeleton

As a first step we always have to create a Springboot project skeleton using Online Spring Initialiser (https://start.spring.io — Create a project similar to below approach and generate and download and extract then load it into IDE) or IntelliJ IDE (We can create using other IDE’s also but I will be using IntelliJ), and once we create any project successfully its always important to have it added into GitHub or any distributed code repositories.

Project initial naming configuration
Dependencies Selected

Let’s add project into GitHub repository: Once project is loaded into IDE, as a one time activity, we need to click on ‘Create Git repository’ and ‘share it into GitHub repository’ by logging into your GitHub account (I have used GitHub, feel free to use others).

Creating Local Repository
Adding Project into GitHub

Now let’s review important build file (build.gradle) which does the magic of getting dependencies for our project and do the compile, build and deploy activities smoothly in the background.

Step2: Create Routes (End points) Configuration Layer

Now lets start creating endpoints configuration bean class, which will build endpoint config, it is more of a simplified and better readable class to have all endpoints configured at one place and invoke implementing Handler layer (also known as controller layer).

Routes Configuration Layer

Step3: Create Handler Layer

Create a Handler layer (In Spring Reactive world we call Controller layer as Handler layer) which is a kind of request and response processing layer by calling Service layer.

Handler Layer class

Step4: Create Service Layer

Create a Service Layer which will produce simple ‘Hello World!’ text and give it back to Handler layer. Generally Service layer integrates with DB or Another Microservices or Remote file processing or Cloud S3 or any other storage services to serve the externally processed data and send back to Handler layer.

Service Layer Class

Testing

Automation Testing places a vital role in todays microservices testing, there are many phases of testing involved like Unit Testing (Verify specific code block), Integration Testing (with DB, other micros Services or Stub testing etc), V&P Testing (Volume and Performance) etc…, It’s always good to test whatever we have written, it gives you great confidence with 100% code coverage in SonarQube like code coverage tools statistics, Here I am going to cover Routes, Handler and Service Layers Unit testing in this project story and you will see more advanced ways of testing in upcoming stories.

I will be using Spoc Framework for testing which is a very good enterprise level testing approach, Its interactive, Data driven test approach, which is inspired from JUnit, jMock, RSpec, Groovy, Scala, Vulcans, and other fascinating life forms.

Step1: Lets first configure ‘Spoc’

Since Spoc is built on top on Groovy, as defined in build file, we need Groovy Plugin, BOM (Bill of Materials) where Spoc related dependencies are located, spoc-core and spoc-spring.

Now existing ‘test/java/….’ need to be removed (Using below Project Structure we can delete it) and while you creating a test class for your source file ‘test/groovy/…’ directory will automatically gets created and ‘<sourcefilename.groovy>’ will be created then you only need to create ‘definitions’ as it appears in code.

Handling test directory structure
Create Test Classes (ctrl+enter(Mac)/alt+enter(Windows)).

Step2: Lets test context load is OK or not by loading any bean.

Context Bean Load Test

Step3: Test Handler Layer

HelloWorldHandlerTest

Step4: Test Service Layer

HelloWorldServiceTest

GitHub project is located athttps://github.com/babugsuresh/microservices-part1.git

Upcoming Topic..

Microservices(Part2)-Logging, Dockerizing & API Specification with Swagger

--

--

Suresh Babu G

Software Architect & Senior Cloud Java Consultant with 14+ years of experience in the IT industry || Certified Oracle Java, AWS, IBM Cloud, Azure Engineer.