How to specify path of WireMock mappings and files present inside executable Spring Boot Jar?

Viewed 6282

I have built WireMock as a spring boot application using "spring-cloud-contract-wiremock" dependency. I have kept the mapping and _files inside src/test/resources folder.

When I run the application from IDE it works perfectly. But when I run the Jar directly it gives a fileNotFoundException.

<pre>java.lang.RuntimeException: java.io.FileNotFoundException: C:\Users\{user}\Downloads\src\test\resources\__files\....

My application.yml file contents :

spring:
main:
    web-application-type: none
  application:
    name: wiremock-service
    
wiremock:
  server:
    files: classpath:/__files
    stubs: classpath:/mappings

Main class :

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Bean
    public Options wireMockOptions() throws IOException {

        final WireMockConfiguration options = WireMockSpring.options();
        //options.withRootDirectory("classpath*:");
        options.port(9990);

        return options;
    }

}

How do I specify the path for _files and mappings so that I can run the independently from any location?

4 Answers

Thanks everyone for your suggestions.

I was able to make it work by adding below in my Main class

FileSource fileSource = new SpringClasspathResourceFileSource("stub");
options.fileSource(fileSource);

SpringClasspathResourceFileSource is a normal class implementing FileSource interface. "stub" is a folder that I created inside src/main/resources. The fileSource object has return the uri of your folder from your application's classpath.

You can change the path of where WireMock looks for /mappings and /__files by using one of these file location methods.

    public Options wireMockOptions() throws IOException {

        final WireMockConfiguration options = WireMockSpring.options();
        options.usingFilesUnderDirectory("classpath*:");
        options.port(9990);

        return options;
    }

I will admit that I haven't used WireMock with Spring, so things might be a little different here.

If you just normally run the application the test resources will not be found as they are normally not included in the generated jar file. Do you need Wiremock for tests or for actually running the application? If the latter, you need to move the files into the normal resource path 'src/main/resources'.

I had a similar issue running Wiremock without the HTTP Server inside a Spring Boot application. Managed to get it working by prefixing the path with "BOOT-INF/classes/"

Running in an IDE:

usingFilesUnderClasspath("wiremock")

When running from jar:

usingFilesUnderClasspath("BOOT-INF/classes/wiremock")
Related