sugo
sugo

Reputation: 25

Wiremock - how to read response from json file?

I am trying to use wiremock, using this dependency:

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock-jre8</artifactId>
    <version>2.33.2</version>
    <scope>test</scope>
</dependency>

I intend to read responses from json files. However, nothing I do works. The test passes without reading the file (I've attempted to use random file names and it always works). What could be wrong here?

@SpringBootTest
@TestExecutionListeners
@ContextConfiguration(initializers = [WireMockContextInitializer::class])
@AutoConfigureWebTestClient
class EndpointsTest {

    @Autowired
    private lateinit var wireMockServer: WireMockServer

    @AfterEach
    fun afterEach() {
        wireMockServer.resetAll()
    }

    @Test
    fun anything() {
        wireMockServer = WireMockServer()
        wireMockServer.stubFor(post("/some-endpoint").willReturn(aResponse().withBodyFile("something.json")))
        ...
    }
}

Upvotes: 1

Views: 7717

Answers (1)

agoff
agoff

Reputation: 7125

WireMock has a specific path requirement.

To read the body content from a file, place the file under the __files directory. By default this is expected to be under src/test/resources when running from the JUnit rule.

Make sure your file is located at src/test/resources/__files/something.json.

Upvotes: 2

Related Questions