How to test microservices



This article discusses simplified ways to test microservices architecture-based applications. As we know that in a microservice architecture, the entire application is divided into smaller loosely coupled components called microservices. Each microservice acts as a small individual application, each having its own DB. These microservices communicate with each other through APIs.

microservices based architecture

Advantages of having microservice-based architecture:

1. Easy and faster to scale.
2. If one microservice is down, then it doesn't halt the entire system or application.
3. Deployment is relatively easy.


To test microservices architecture-based applications, we need to consider the below testing phases:

Unit testing: The developers must ensure that every piece of code component works as expected. Unit testing is the first step to check the accuracy of the individual modules.

Individual component testing: In this phase, we validate the individual component is working fine in isolation. From a testing point of view, ensure that every endpoint is touched upon. Prepare the UI test scenarios that cover every individual service working in an underlying component. In this, you are validating the request and response of a component in isolation.

test cases for microservices testing


Integration testing: As we know that microservices communicate with each other through APIs. Thus, API testing plays an important role in this phase. In this testing phase, we need to validate the interfacing and integration aspects of the microservices. The goal of integration testing is to ensure the data flow from one component to another, in other words, validate how the components work together. Prepare the test scenarios that cover the interaction points of the components.
For e.g. Adding to cart --> Placing the order --> Checking the previous orders
Here, you are validating the request and response of components working together.

The following tools can be used for API testing:

1. Postman
2. Java RestAssured API
3. SoupUI

Contract testing: Contract testing is an enhanced form of integration testing. It is a methodology that captures the communication between two independent systems (microservices). The captured interaction is stored in a contract and later the validation is performed to check whether the systems are adhering to this contract or not.

End to end testing: E2E testing should cover the entire logical flow of the application from start to end. E2E testing is built on integration testing and it should cover the complete functional paths of the application from one start to end.

How to write test cases for testing microservices


Understand, what each microservice is supposed to do and figure out the endpoints. Many a time, the dev team prepare the detailed doc that contains the endpoints, request URLs, their functionalities, required payload, headers details, request types, etc. But in the absence of that, we need to take out these details by manually performing the UI operations on the browser and fetching the details through the browser's network tab. The key performance indicators from this doc should be the response data that came out after executing a specified set of scenarios.

List down the scenarios, and identify the request URL, header details, payload (test data), expected response, and actual response. Once all these details are identified, prepare a test case doc with the specified headers as shown below:

test cases for microservices testing



Executing the API tests through Java rest-assured


1. First things first. Add all the required rest-assured dependencies in pom.xml



2. You need to perform the authentication before performing the API tests. You can perform the cookie-based authentication in case your app is using cookies for token id or auth id. For this write a UI test using Selenium and Java and perform the login operation. Now once the login is done, you can fetch the cookies using Selenium's getCookies() function, store them in a set, and return the set of cookies in your API tests :

Set s=driver.manage().getCookies();


3. In the API test, define the base URI:

RestAssured.baseURI="https://stage.demo.co";


4. Create request and response objects, pass the cookies in the headers:

RequestSpecification req=RestAssured.given();
req.header("Content-Type","application/json");
Iterator r=s.iterator();

while(r.hasNext()) {
try {
req.header("cookie",r.next().toString());
}
catch(Exception e) {
test.log(LogStatus.FAIL, "Failed to pass the cookies to the request headers" +e.getMessage());
}
}

5. Pass the payload in the request:

req.body("{\"permissions\":true,\"isLive\":true,\"isActive\":true,\"isVendorContactActive\":true,\"isDeliveryActive\":true,\"companyName\":\""+testDemo+"\",\"linkPerma\":\""+ran+"perma\",\"suffix\":\""+threeCharcterPrefix+"\",\"calcAPIKey\":\"t\",\"calcEmail\":\""+ran+"@test.co\",\"vendorCode\":\"testgrpcode\",\"priceLevel\":\"3.00\",\"shortDescription\":\"test short descp\",\"sourcing\":\"test sourcing disclaimer \",\"fullDescription\":\"test full descp\",\"tips\":\"test alergen disclaimer \",\"titleMeta\":\"test meta title for seo\",\"descriptionMeta\":\"test meta descption\",\"quote\":\"test statement\",\"authorQuote\":\"jayeshmanager\"}");


6. Send the request using the request object, and store the response in the response object.

res=req.request(Method.POST,"/api/create/admin/supplier");


7. Analyze the response :

res.getStatusCode()
res.getBody().asString()
res.asPrettyString()


8. Also, fetch the required data from the response that can be passed in the subsequent request:

String suppID=res.getBody().jsonPath().getString("payload");