[英]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.