19

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.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Ragini
  • 1,509
  • 7
  • 28
  • 42
  • According to http://wiremock.org/docs/configuration/ you should be able to change the resources location. Keep in mind this changes the location of all files within the `resources` directory. – GuyT Jan 11 '18 at 09:57
  • What is the reason you want to have a different location? – A. Kootstra Aug 27 '18 at 11:14
  • @A.Kootstra - Why do you ask? – MasterJoe Sep 11 '20 at 06:19
  • Because that reason may be solved in another manner, which is something that should be looked at given that this is hard-coded in the software. – A. Kootstra Sep 12 '20 at 08:40

4 Answers4

2

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
Arthur Kazemi
  • 960
  • 1
  • 10
  • 11
0

For Java & Spock but still using __files folder

@TestConfiguration
public class WireMockConfig {

    @Bean
    public WireMockConfigurationCustomizer wireMockConfigurationCustomizer() {
        return config -> {
            config.withRootDirectory("src/integration-test/resources");
        };
    }
}

Then where ever you have ApplicationContext initializer :

@SpringBootTest(classes = [Application.class, WireMockConfig.class], webEnvironment = RANDOM_PORT)
@ContextConfiguration()
abstract class WireMockIntegrationSpec extends Specification {
}

Then in your test :

@AutoConfigureWireMock(port = 9089)
@Unroll
class ApplicationSpec extends WireMockIntegrationSpec {

    def "Some test" () {
    }
}

Your resources will be served from src/integration-test/resources/__files. Make sure this directory structure exists

Piotr Dajlido
  • 1,982
  • 15
  • 28
0

It cannot be changed. You always need a "__files" dir. See this question and the comment by @luckyluke.

But the part before that can be changed.

It can be done like:

WireMockServer wireMockServer = new WireMockServer(
    wireMockConfiguration.options()
        .usingFilesUnderDirectory("src/test/resources/something")
);

Or, without changing the src/test/resources part, only appending more levels under it:

wireMockServer = new WireMockServer(8090, new ClasspathFileSource("integration/responses"), false);

The logic used by Wiremock is not very straightforward, but I will put my findings here:

  • Wiremock will get the default root dir, which is src/test/resources(or other path specified in the 1st approach); or, if you use 2nd approach above, Wiremock will append that part after src/test/resources.
  • Wiremock will then append "__files" part, which cannot be changed.
  • It reads the value in "withBodyFile(value)", and join all to form a Path.

So, for example, if I want to read job-resource.json under src/test/resources/integration/responses, I need to create a __files under this dir, put the json into it, launch the wiremock server in the 2nd approach, and then:

wiremock.stubFor(
    get(urlPathMatching("xxx"))
            .withId(stubId)
            .willReturn(
                    aResponse()
                            .withStatus(200)
                            .withBodyFile("job-resource.json") // note here it's just filename, not "__files/job-resource.json"
            );
);
WesternGun
  • 11,303
  • 6
  • 88
  • 157
-1

This seems to be something you need to configure when creating the rule. Try

@Rule
public final WireMockRule rule = new WireMockRule(WireMockConfiguration.wireMockConfig()
  .withRootDirectory("src/test/resources/Testing_ABC"));

and then you probably can use .withBodyFile("Testcase2/myfile.xml) in test.

Henri Viik
  • 664
  • 1
  • 5
  • 16
  • 2
    Thanks for your response Henri. I tried what u suggested. But it looks like wiremock searches the files in src/test/resources/Testing_ABC/__files/Testcase2/myfile.xml. So it expects __files directory somehow, which I would like to avoid. – Ragini Jan 11 '18 at 10:17