Skip to main content

Wiremock stateful behaviour

In distributed systems architecture, testing becomes much more complex than in monolith architecture. Although only end-to-end testing gives us full confidence of system-wide implementations, we should also test services in isolation. The easiest way to obtain isolation in microservices architecture is to stub external applications.

The problem is that mocks are usually stateless – they simulate application logic with fixed actions, without any conditional behaviour. If we want to test cases like HTTP latency or data redundancy validations, where external service represents different behaviour among repeated calls, simple mocks can be insufficient.

Wiremock, mocking tool which I described already in previous posts, provides simple yet efficient feature that address those challenges, called stateful scenarios. The idea is to declare different responses for one method, and mark every one of them with tags representing scenario states. When you perform a request, then you also set your mock in certain state.

Example scenario 1 – data validation 

Imagine that you want to test your application against external service. The scenario is to POST user data to external endpoint. First request should result in response with newly created user ID. Then, If you post the same user’s data again, you should see response code 400 with user already exists exception. Wiremock mapping example is:


…and second state behaviour is:

Example scenario 2 – network latency 

Every developer should be aware of HTTP network issues. In second scenario, we’d like to test our application timeout policy: We will perform GET request on external service – first one should respond with 5sec delay, and the second should succeed without any latency. Example for wiremock stub is:


…and second state behaviour is:

Continue reading 

If you want to continue reading and expand your knowledge in area of REST and microservices, I recommend you these books:

  • Building Microservices – one of the most important books for me, everything you want to know about microservices is here
  • Java For Testers: Learn Java fundamentals fast – test automation does not require complex programming knowledge. Learn fundamentals of Java for test automation. From tester to testers!
  • RESTful Web APIs – another great book about REST architecture. Lots of practical knowledge about designing and consuming RESTful APIs

Summary 

As you can see on the code examples, we’re not faking application logic, we’re just simulate it’s states. In above scenarios, we limit ourselves to two states, but it’s basically up to us how many we’ll provide. Wiremock is pretty flexible in terms of HTTP calls simulation, so you can experiment with more composite scenarios. I can also recommend official documentation.

Popular posts from this blog

Testing Asynchronous APIs: Awaitility tutorial

Despite the growing popularity of test automation, most of it is still likely to be done on the frontend side of application. While GUI is a single layer that puts all the pieces together, focusing your automation efforts on the backend side requires dealing with distributed calls, concurrency, handling their diversity and integration. Backend test automation is especially popular in the microservices architecture, with testing REST API’s. I’ve noticed that dealing with asynchronous events is particularly considered as challenging. In this article I want to cover basic usage of Awaitility – simple java library for testing asynchronous events. All the code examples are written in groovy and our REST client is Rest-Assured. Synchronous vs Asynchronous  In simple words, synchronous communication is when the API calls are dependent and their order matters, while asynchronous communication is when the API calls are independent. Quoting Apigee definition: Synchronous  If a

Rerun Flaky Tests – Spock Retry

One question I get asked a lot is how you can automatically rerun your test on failure. This is a typical case for heavy, functional test scenarios, which are often flaky. While test flakiness and its management is crucial and extensive matter itself, in this post I want to give a shout to the extremely simple yet useful library: Spock-Retry. It introduce possibility to create retry policies for Spock tests, without any additional custom-rules implementation – just one annotation. If you are not a fan of Spock testing framework and you prefer JUnit – stay tuned! I will post analogous bit about rerunning JUnit tests soon. Instalation  If you are an maven user, add following dependency: <dependency>       < groupId > com.anotherchrisberry < /groupId >       < artifactId > spock-retry < /artifactId >       < version > 0.6.2 < /version >       < type > pom < /type >   </dependency> For gradle users:

Performance Testing – Vegeta Attack!

Performance testing is crucial field of modern software development. Taking into account that in today’s world majority of app’s communication is web-based, it turns out to be even more important. However, it still enjoys less interest than automated functional testing, and publications on load testing subject usually focus on mature and complex tools like JMeter or Gatling. In this post I’d like to introduce command line usage of a super simple and lightweight tool for performance testing of HTTP services, which is Vegeta. Who is Vegeta  Besides Dragon Ball’s character, Vegeta is a simple load testing tool written in GO, that can be used both from command line and as an external library in your project. In order to use Vegeta, download executables from here . It is recommended to set environment variable for ease of use.  For Mac users, you can download vegeta directly from Homebrew repositories: $ brew update && brew install vegeta Vegeta’s Arsenal  Usage