Mario Niebes
02/21/2025, 10:13 AM"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
.
{
"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 🙏Tom
02/21/2025, 2:06 PMMario Niebes
02/22/2025, 4:01 PM__files/response-POST-api-v1-users.201.json
is
{
"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
{
"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
{
"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
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:
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
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
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 parametersTom
02/24/2025, 10:21 AMmappings/
from the path passed to the ClasspathFileSource constructor, as this path is the parent of mappings and __files.Mario Niebes
02/24/2025, 9:17 PMmappingSource
and withStores
with just withRootDirectory
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!