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