DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • How To Create a Stub in 5 Minutes
  • How to Do API Testing?
  • What Is API-First?
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)

Trending

  • Java Collection Overhead
  • Apply Strangler Pattern To Decompose Legacy System Into Microservices: Part 1
  • Introducing the Apache JMeter Docker Extension
  • Data Consistency in Distributed Systems: Transactional Outbox
  1. DZone
  2. Data Engineering
  3. Databases
  4. Wiremock and Response Transformer

Wiremock and Response Transformer

This article will help you learn about Wiremock's Response Transformer and how to hook it into your tests and use it to modify responses.

Ravi Isnab user avatar by
Ravi Isnab
·
Nov. 09, 17 · Tutorial
Like (3)
Save
Tweet
Share
13.3K Views

Join the DZone community and get the full member experience.

Join For Free

I wanted to test a scenario where:

  • For the first x calls, respond with y
  • Then, for x+1, respond with z

Wiremock by itself doesn't provide a direct way to stub different responses akin to Mockito's thenReturn or Spock's multiple stubs. Wiremock, however, provides a ResponseTransformer. I used Wiremock's ResponseTransformer to store state, and return y by default but return z on x+1 try.

  static class SuccessfulAtLastTransformer extends ` {

        int count = 1

        SuccessfulAtLastTransformer() {}

        @Override
        ResponseDefinition transform(
                final Request request, final ResponseDefinition responseDefinition, final FileSource fileSource) {

            if (request.getUrl().contains("/customer/cust1234")) {
                if (count == 3) {
                    def responseJson = new JsonSlurper().parseText(responseDefinition.body)
                    // Set the customerId on the final try
                    responseJson.customerCar = "toyota"
                    def newRequestBody = JsonOutput.toJson(responseJson)
                    return new ResponseDefinitionBuilder()
                            .withStatus(200)
                            .withBody(newRequestBody)
                            .build()
                }

                return new ResponseDefinitionBuilder()
                        .withHeader("COUNT", count++ as String)
                        .withStatus(200)
                        .withBody(responseDefinition.body)
                        .build()
            }

            return responseDefinition
        }

        @Override
        boolean applyGlobally() {
            return false
        }

        @Override
        String name() {
            return "SuccessfulAtLastTransformer"
        }
    }

Line #1: extend import com.github.tomakehurst.wiremock.extension.ResponseTransformer

Line #8: Override the transform function.

Line #11: Check if this is the API you are interested in.

Line #12: Check if the count has reached a certain threshold, and if so, then add customerCar property to response JSON.

Line #23: Add count to the response header. That acts as our state storage. Once the count will reach x in this case 3, then change the response (line #13).

Line #34: Set apply globally false, i.e. don't use this ResponseTransformer as an interceptor anywhere else but where I put it.

Line #40: Give your transformer a name.

Once you have defined a ResponseTransformer like above, you can then attach it to the Wiremock instance, like so:

@Shared  
def customerServiceMock =  
      wireMockConfig()
     .port(8090)
     .extensions(SuccessfulAtLastTransformer)

Done.

Please let me know if you have any questions or if there's a better way to do the same thing in Wiremock via comments on this post.

Stub (distributed computing) POST (HTTP) Testing Mockito Property (programming) API JSON

Published at DZone with permission of Ravi Isnab, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Create a Stub in 5 Minutes
  • How to Do API Testing?
  • What Is API-First?
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: