Microservices E2E Tests

Victor Krapivin
2 min readApr 14, 2022

--

Here I’ll describe our approach to do e2e tests for Microservices application. I hope it will be helpful.

Microservices communicates to each other via Kafka or REST API. Also they call some external APIs.

We run e2e test at special e2e stage within CD pipeline, after successful build and successful deploy onto e2e environment.

Our first approach to run e2e tests was pretty stupid — just run ALL e2e test for every pipeline run. But only e2e stage within CD pipeline took more than 25–30 minutes to run all e2e tests. It was really toooooo long time. And we’ve changed our approach to run e2e tests.

Do tests with E2E Interaction Path

Lets take a typical test case:

when customer order accepted by manager then then stock reservation occurs”.

Microservices interact to each other to perform business logic for that scenario. This is an interaction path. For example:

  • Within order creation action there is an API interaction between Orders and ProductsCatalog microservices, also there could be interaction between Orders and Customers Service.
  • When order accepted by manager then there is an interaction between Orders and Stock microservices to reserve product in the stock.

Interaction paths are tested within e2e test.

Our typical test for interaction path looks like following (we are using JUnit to write e2e tests):

public class OrderToStockReservationInteractionPath {
@Test
public void when_order_accepted_then_stock_reservation_occurs() {
// arrange
Product product = aProduct(SONY_PS2);

havingProductInTheCatalog(product);
havingProductStockLevel(product, quantity(10))

Order order = havingCustomerOrder(
anOrder()
.fromCustomer(aCustomer())
.withOrderLine(product, quantity(3)));

// act
managerAcceptsOrder(order);

// assert
awaitWithTimeout(ofSeconds(20))
.until(productReservationOccurs(product, quantity(3));
}
}

A number of interaction paths may go through particular microservice. For every microservice we have a test suite defined. Every interaction path that goes through microservice should be included into such suite. For example:

@RunWith(Suite.class)
@Suite.SuiteClasses({
ShopingCartCheckoutToOrderInteractionPath.class,
OrderToStockReservationInteractionPath.class,
...
})
public class OrdersMicroserviceE2eTestsSuite {
}

When something changed in the Orders microservice then new CD pipeline will be scheduled. On the e2e tests stage of the CD pipeline we have a step to run OrdersMicroserviceE2eTestsSuite suite. So all e2e interactions paths are going through that microservice will be tested.

And now we have e2e tests stage less than 3 minutes.

--

--