简体   繁体   English

Spring 启动集成测试错误:“无法解析占位符 'wiremock.server.port'”在不需要 Wiremock 的测试中

[英]Spring boot integration test error: “Could not resolve placeholder 'wiremock.server.port'” in tests not requiring Wiremock

I have a @SpringBootTest which tests the loading of a @ConfigurationProperties and its methods.我有一个@SpringBootTest来测试@ConfigurationProperties及其方法的加载。 It is in test source set.它在test源集中。

And, in itest , I have an integration test which uses Wiremock(sending requests and using stubs as response, etc.)而且,在itest中,我有一个使用 Wiremock 的集成测试(发送请求并使用存根作为响应等)

Now, when I run gradle test , the first test fails, saying:现在,当我运行gradle test时,第一个测试失败,说:

[ENV=local] [productName=app-gateway-api] [2019-10-22T16:18:30.994Z] [ERROR] [MSG=[Test worker] osboot.SpringApplication - Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController' defined in file [E:\coding\code\app\build\classes\java\main\com\app\controller\MyController>.class]: Unsatisfied dependency expressed through constructor parameter 0; [ENV=local] [productName=app-gateway-api] [2019-10-22T16:18:30.994Z] [ERROR] [MSG=[Test worker] osboot.SpringApplication - 应用程序运行失败 org.springframework.beans.factory .UnsatisfiedDependencyException:创建文件 [E:\coding\code\app\build\classes\java\main\com\app\controller\MyController>.class] 中定义的名称为“myController”的 bean 时出错:通过构造函数参数表示的不满足依赖项0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proxyService': Injection of autowired dependencies failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“proxyService”的 bean 时出错:注入自动装配的依赖项失败; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'wiremock.server.port' in value " http://localhost :${wiremock.server.port}/send"嵌套异常是 java.lang.IllegalArgumentException:无法解析值“ http://localhost :${wiremock.server.port}/send”中的占位符“wiremock.server.port”

Which is used in some external properties.用于一些外部属性。 The value will be used in itest , but not in test .该值将在itest中使用,但不在test中。 But the context loads it always.但是上下文总是加载它。

In other projects using it like this, there is no problem.在其他像这样使用它的项目中,没有问题。 But, it seems that it is loading all the classes and cannot find Wiremock and create the server.但是,它似乎正在加载所有类并且找不到 Wiremock 并创建服务器。

The test in problem:有问题的测试:

@SpringBootTest
public class MapperLookupTest {
    ...
}

Wiremock dependency already has compile scope: Wiremock 依赖已经compile了 scope:

    compile('com.github.tomakehurst:wiremock-jre8-standalone:2.21.0')
    compile("org.springframework.cloud:spring-cloud-starter-contract-stub-runner")

I have tried to load only the necessary classes with @SpringBootTest(classes = {...}) , but it is too verbose.我试图用@SpringBootTest(classes = {...})只加载必要的类,但它太冗长了。

So, is there any easy way to tell the context to load Wiremock?那么,有什么简单的方法可以告诉上下文加载 Wiremock 吗?

Just add a default value to your placeholder:只需为占位符添加一个默认值:

${wiremock.server.port:defaultValue}

At last I decide not to honor @Cwrwhaf, sorry.. because I don't want to have a possible negative impact on the production code;最后我决定不尊重@Cwrwhaf,对不起..因为我不想对生产代码产生可能的负面影响; I just want to pass the test and any changes should be limited to the mere scope of the test itself.我只想通过测试,任何更改都应仅限于测试本身的 scope。

So I did:所以我做了:

@SpringBootTest(property = "wiremock.server.port=8080")

And I am aware of the fact that it is not the perfect answer, but has less impact than changing global settings of wiremock.而且我知道这不是完美的答案,但与更改wiremock的全局设置相比影响较小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 使用 WireMock 和 Eureka 进行引导集成测试失败并显示“没有可用的实例” - Spring Boot Integration Test with WireMock and Eureka fails with "No instances available" 在 spring 启动测试中使用 wiremock 随机端口设置属性 - Set property with wiremock random port in spring boot test 使用外部 Https 主机在 Spring 上进行 WireMock 启动测试 - WireMock On Spring Boot Test With External Https Host Spring 启动测试中的@Value“无法解析占位符” - @Value "Could not resolve placeholder" in Spring Boot Test Java集成测试中的java.net.SocketTimeoutException,同时使用带有Spring Boot和Junit的Wiremock - java.net.SocketTimeoutException in integration test in Java while using Wiremock using Spring Boot with Junit 无法设置 wiremock 服务器端口 - Unable to set wiremock server port Spring 启动 JUnit 测试 - 如何从 Wiremock 返回响应 - Spring Boot JUnit Test - How to return response from Wiremock Spring Boot Cucumber 测试无法解析占位符“random.uuid” - Spring Boot Cucumber tests could not resolve placeholder 'random.uuid' 如何在响应正文中返回 WireMock 服务器端口 - How to return WireMock server port in a response body 使用 WireMock 的 Spring JUnit 测试中的多个应用程序上下文 - Multiple Application Contexts in Spring JUnit test with WireMock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM