如何指定 WireMock 映射和位于可执行的 Spring Boot Jar 内的文件的路径?

huangapple go评论84阅读模式
英文:

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

问题

我已经使用"spring-cloud-contract-wiremock"依赖将WireMock构建为Spring Boot应用程序。
我将映射和_files文件夹放在了src/test/resources文件夹中。

当我从IDE运行应用程序时,它能够正常工作。但是当我直接运行JAR文件时,会出现FileNotFoundException异常。

我的application.yml文件内容如下:

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

主类如下:

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;
    }

}

我应该如何指定_files和mappings的路径,以便可以在任何位置独立运行它们?

英文:

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?

答案1

得分: 5

感谢大家的建议。

通过在我的主类中添加以下内容,我成功使它运行起来了:

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

SpringClasspathResourceFileSource 是一个普通的类,实现了 FileSource 接口。
"stub" 是我在 src/main/resources 目录下创建的文件夹。fileSource 对象已经从应用程序的类路径中返回了您的文件夹的 URI。

英文:

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.

答案2

得分: 3

我在一个Spring Boot应用程序中运行Wiremock时遇到了类似的问题,即在没有HTTP服务器的情况下运行Wiremock。通过在路径前加上"BOOT-INF/classes/",成功使其工作。

在IDE中运行:

usingFilesUnderClasspath("wiremock")

从JAR文件运行时:

usingFilesUnderClasspath("BOOT-INF/classes/wiremock")
英文:

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")

答案3

得分: 2

你可以通过使用其中一种文件位置方法来更改 WireMock 查找 /mappings/__files 的路径。

    public Options wireMockOptions() throws IOException {

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

        return options;
    }

我要承认我并没有在 Spring 中使用过 WireMock,所以这里可能会有些不同。

英文:

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.

答案4

得分: 1

如果您只是正常运行应用程序,测试资源将无法找到,因为它们通常不包括在生成的JAR文件中。您是需要用于测试还是实际运行应用程序?如果是后者,您需要将这些文件移动到正常的资源路径 'src/main/resources'。

英文:

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

huangapple
  • 本文由 发表于 2020年7月24日 15:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63069199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定