Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing with Wiremock - WithBodyFile Location other than _files

Wiremock Documentation states that the location of the file specified in withBodyFile should be in src/test/resources/__files. I would like to have file in src/test/resources/Testing_ABC/Testcase2/myfile.xml.

Is there any way I can achieve this ? I tried following, but it does not seem to work !

stubFor(get(urlPathEqualTo("/abc")).willReturn
                (aResponse().withHeader("Content-Type",
                        "text/xml; charset=utf-8").withHeader
                        ("Content-Encoding",
                                "gzip")
                        .withBodyFile
                                ("src/test/resources/Testing_ABC/Testcase2/myfile.xml)));

However, when I put my file in src/test/resources/__files/myfile.xml and change the path accordingly, it works fine.

I am just wondering if I can make wiremock look in some other directory in resources other than __files just in order to have nice resource structure in project.

like image 258
Ragini Avatar asked Jan 11 '18 09:01

Ragini


People also ask

What is WireMock stubbing?

What Is Stubbing? Stubbing is a technique that allows us to configure the HTTP response that is returned by our WireMock server when it receives a specific HTTP request. We can stub HTTP requests with WireMock by using the static givenThat() method of the WireMock class.

How do you throw an exception with WireMock?

Let's say your API returns BadRequest (http code 400) in case of exception UserNotFoundException: @PostMapping(value = "/test/user/get") public String myApi(@RequestParam String param1) { try {... } catch(UserNotFoundException e) { throw new ResponseStatusException(HttpStatus. BAD_REQUEST, "user not found");

What is WireMock rule?

WireMock is a tool that can mimic the behavior of an HTTP API and capture the HTTP requests send to that API. It allows us to: Configure the response returned by the HTTP API when it receives a specific request. Capture the incoming HTTP requests and write assertions for the captured HTTP requests.

How do I reset my WireMock?

Reset. The WireMock server can be reset at any time, removing all stub mappings and deleting the request log. If you're using either of the JUnit rules this will happen automatically at the start of every test case. However you can do it yourself via a call to WireMock.


1 Answers

I'm using this Kotlin Config class to customize the Root Directory. It still requires response file to be in the __files directory.

@Configuration
class Config: WireMockConfigurationCustomizer {
    override fun customize(config: WireMockConfiguration?) {
        config!!.withRootDirectory("customer-client/src/test/resources")
    }
}

@AutoConfigureWireMock
like image 101
Arthur Kazemi Avatar answered Sep 23 '22 07:09

Arthur Kazemi