WireMock JUnit 5 and Rest-Assured Example

WireMock JUnit 5 and Rest-Assured tutorial consists of a small demo about Wiremock and JUnit 5. We can create mocks and stubs with standalone or without. In this article, we will not go with the standalone solution. Thus, we do not need to install WireMock standalone .jar file, did the required settings and start it before test execution. We will cover this another article. In this article, we will handle all the required operations in our project.

First, you need to create a maven project and name it “wiremock-example“.

Here is our project’s pom.xml:

We use WireMock for mocking and stubbing, Rest-Assured for HTTP Client Test API, and JUnit 5 as a test runner framework. Below you can see our project structure:

wiremock junit 5

As you see the above screenshot, we have a glossary.json file. In that file, I hold my stub’s body as shown below. IT should be located under: resources -> __files -> json directory.

By the way, you can validate your JSON files on http://jsonpath.com/ website. In this article, we will check the title as an example. Its json path is shown below:

wiremock rest assured

We can create a stub and run sample tests in below code. Don’t worry, I will explain the critical parts of the code. 😉

First, we need to declare a Wiremock instance with below line.

Then, we need to start the mock server on a specific port and set up the stub for each test.

We need to set up our stub as follows.

  • We need to declare an endpoint url.
  • You can define your content type.
  • Define your status.
  • And point your body as “json/glossary.json” which ve prepared before by using .withBodyFile method.

And at the end of each test, we should close the mock server.

The rest of the stuff in the project code is Rest-Assured test methods. You can get more information about rest-assured here.

Project link: https://github.com/swtestacademy/junit5-wiremock-example

Thanks for reading.
Onur Baskirt

By |2018-12-05T10:51:06+00:00October 3rd, 2018|Api Testing|0 Comments

About the Author:

Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as a research assistant, design verification engineer, and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. After Teknosa, he worked as Head of Software Testing and Manager of two Development Teams at Kariyer.net. Now, he is working as a Senior Technical Consultant at Emirates Airlines in Dubai. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.