Hey, thanks for the nice library! I've mocked a re...
# help
m
Hey, thanks for the nice library! I've mocked a response and now want to add a dynamic ID into it. From what I understand this works by adding
"transformers": ["response-template"],
into the mapping and the placeholder (e.g.
{{randomValue type='UUID'}}
) into the response itself. I put
mapping
and
__files
in a subfolder
myservice/src/test/resources/api/client/mockedapi/__files/response-POST-api-v1-users.201.json
and
myservice/src/test/resources/api/client/mockedapi/mappings/mapping-POST-api-v1-users.201.json
.
Copy code
{
  "priority": 100,
  "request": {
    "urlPath": "/api/v1/users",
    "method": "POST"
  },
  "response": {
    "status": 201,
    "bodyFileName": "response-POST-api-v1-users.201.json",
    "headers": {
      "Content-Type": "application/json"
    }
  }
}
When adding the transformer attribute
"transformers": ["response-template"]
, the response file is not found anymore as it tries to find the
__files
in the test resource root
myservice/src/test/resources/__files/response-POST-api-v1-users.201.json
I'm assuming I'm using it wrong, but what is the right config? Help is appreciated 🙏
t
Can you share the final stub JSON and the exact input and response you’re seeing?
m
initially the
__files/response-POST-api-v1-users.201.json
is
Copy code
{
  "id": "7a14af18-f28b-425e-bc4c-d181498fa0ba",
  "password": "9f75b626-9e39-4720-9ffd-b8215f21d45b",
  "email": "<mailto:test@example.com|test@example.com>"
}
then with templating it's
Copy code
{
  "id": "{{randomValue type='UUID'}}",
  "password": "9f75b626-9e39-4720-9ffd-b8215f21d45b",
  "email": "<mailto:test@example.com|test@example.com>"
}
with an added
"transformers": ["response-template"],
in the mapping
mappings/mapping-POST-api-v1-users.201.json
Copy code
{
  "priority": 100,
  "request": {
    "urlPath": "/api/v1/users",
    "method": "POST"
  },
  "response": {
    "status": 201,
    "bodyFileName": "response-POST-api-v1-users.201.json",
    "transformers": [
      "response-template"
    ],
    "headers": {
      "Content-Type": "application/json"
    }
  }
}
when started via docker-compose
Copy code
wiremock:
  image: "wiremock/wiremock:3.9.1"
  ports:
    - "18080:8080"
  volumes:
    - ./src/test/resources/api/client/mockedapi/:/home/wiremock/
  entrypoint: [
    "/docker-entrypoint.sh",
    "--global-response-templating",
    "--disable-gzip",
    "--verbose"
  ]
I get the right response:
Copy code
http POST localhost:18080/api/v1/users
HTTP/1.1 201 Created
Content-Type: application/json
Matched-Stub-Id: de589e05-6073-4f76-ae10-187439653c71
Transfer-Encoding: chunked

{
    "email": "<mailto:test@example.com|test@example.com>",
    "id": "bfbae7a3-9f68-4d1b-9580-904da27f906d",
    "password": "9f75b626-9e39-4720-9ffd-b8215f21d45b"
}
but doesn't find the response template when started via tomakehurst
Copy code
import com.github.tomakehurst.wiremock.common.ClasspathFileSource
import com.github.tomakehurst.wiremock.common.filemaker.FilenameMaker
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource
import com.github.tomakehurst.wiremock.store.DefaultStores

object WiremockFactory {
    fun createExtension(path: String): WireMockExtension =
        WireMockExtension
            .newInstance()
            .options(
                wireMockConfig()
                    .dynamicPort()
                    .mappingSource(
                        JsonFileMappingsSource(
                            ClasspathFileSource("$path/mappings/"),
                            FilenameMaker(),
                        ),
                    ).withStores(DefaultStores(ClasspathFileSource(path))),
            ).build()
}
I get
Copy code
http POST <http://localhost:63318/api/v1/users>
HTTP/1.1 500 Server Error
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 934
Content-Type: text/html;charset=iso-8859-1

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 500 java.io.FileNotFoundException: /path/to/myservice/src/test/resources/__files/response-POST-api-v1-users.201.json (No such file or directory)</title>
</head>
<body>
<h2>HTTP ERROR 500 java.io.FileNotFoundException: /path/to/myservice/src/test/resources/__files/response-POST-api-v1-users.201.json (No such file or directory)</h2>
<table>
<tr><th>URI:</th><td><http://localhost:63318/api/v1/users></td></tr>
<tr><th>STATUS:</th><td>500</td></tr>
<tr><th>MESSAGE:</th><td>java.io.FileNotFoundException: /path/to/myservice/src/test/resources/__files/response-POST-api-v1-users.201.json (No such file or directory)</td></tr>
</table>

</body>
</html>
which is using the wrong path Looking at the snippets above I assume I initiate WireMockExtension with wrong parameters
t
I think you need to remove the
mappings/
from the path passed to the ClasspathFileSource constructor, as this path is the parent of mappings and __files.
m
I was able to fix the setup by replacing
mappingSource
and
withStores
with just
withRootDirectory
Copy code
WireMockExtension
            .newInstance()
            .options(
                wireMockConfig()
                    .dynamicPort()
                    .withRootDirectory(filePath(path))
                    .templatingEnabled(true)
                    .globalTemplating(true),
            ).build()
and it works fine with and without templating. Thanks for the help!