element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Dropwizard Integration Testing with Wiremock
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: PEarle
  • Date Created: 9 Apr 2019 1:47 PM Date Created
  • Views 634 views
  • Likes 4 likes
  • Comments 0 comments
Related
Recommended

Dropwizard Integration Testing with Wiremock

PEarle
PEarle
9 Apr 2019

A Dropwizard application will often make a call out to another service - full integration testing therefore requires an instance of that service to be available. THis is not always possible, and for compile time testing (e.g. using JUnit) probably not desirable, as the unavailability of the external service will compromise the completion of tests. Wiremock provides the opportunity to provide mock endpoints which can replicate the external service and enable realistic integration testing without calling an external service.

Test Scenario

For the purpose of this document consider the following example where a service under tests makes a 'smokeTest' communication with a remote service, returning a result indicating whether the remote service is available or not. THe remote service can be considered unavailable if it cant be reached, or returns an invalid status. There are therefore 3 use cases

  • Call an external service which is not running (e.g. HTTP 404)
  • Call an external service which is running but returns an error (e.g. HTTP 500)
  • Call an external service which is running successfully (e.g. HTTP 400)

 

External Service Stub

Wiremock is our friend here. It is relatively simple to create a class which will represent the external interface with Wirock. The following code illustrates how this might be achieve server = new WireMockServer(options().port(0));

server.start();
port = server.port();
configureFor("localhost", port);

stubFor(get(urlEqualTo("/ping"))
.willReturn(aResponse()
                .withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBody("okay"))
);

In the above we do the following ;

Instantiate and start a WiremockServer on port zero. Using port zero means that Wiremock will find an unused port and allocate that - the allocated port can be retrieved.

We then set an expected behavior - in the above instance a call to "/ping" will return a Response with status 200 and a body of "okay" - i.e. the 'happy path'.

Modifying expected behaviour

The simple example above will always return a 'happy path' result - however we may wish to modify this. One simple way would be to amend the class as follows

 

// Define variable to hold the status

private int status;

// Add Constructor Parameter to provide variable response

public MyWiremockServer(int status) {
this.status = status;
}

// Use status in the response (i.e. modify the stubFor definition)

stubFor(get(urlEqualTo("/ping"))
.willReturn(aResponse()
                .withStatus(status)
                ..
);

THe class is constructed with a status which is then used in the stubFor declaration.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube