3

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?

Roshni
  • 137
  • 1
  • 2
  • 11
  • Did you noticed in the error that your app is accessing test folder? `\src\test\resources\` This could be part of issue, ussualy during normal run `\src\main\resources` should be accessed – user2851729 Jul 24 '20 at 07:57
  • Apparently src/test/resources was default location used by WireMockConfiguration class to look for mappings. I had to override the default value by setting the fileSource. – Roshni Jul 25 '20 at 09:25

4 Answers4

5

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.

Roshni
  • 137
  • 1
  • 2
  • 11
3

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")
charlb
  • 1,076
  • 9
  • 18
2

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.

agoff
  • 5,818
  • 1
  • 7
  • 20
1

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'.

Frank Hopkins
  • 639
  • 4
  • 12