繁体   English   中英

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

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

我有一个@SpringBootTest来测试@ConfigurationProperties及其方法的加载。 它在test源集中。

而且,在itest中,我有一个使用 Wiremock 的集成测试(发送请求并使用存根作为响应等)

现在,当我运行gradle test时,第一个测试失败,说:

[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; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“proxyService”的 bean 时出错:注入自动装配的依赖项失败; 嵌套异常是 java.lang.IllegalArgumentException:无法解析值“ http://localhost :${wiremock.server.port}/send”中的占位符“wiremock.server.port”

用于一些外部属性。 该值将在itest中使用,但不在test中。 但是上下文总是加载它。

在其他像这样使用它的项目中,没有问题。 但是,它似乎正在加载所有类并且找不到 Wiremock 并创建服务器。

有问题的测试:

@SpringBootTest
public class MapperLookupTest {
    ...
}

Wiremock 依赖已经compile了 scope:

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

我试图用@SpringBootTest(classes = {...})只加载必要的类,但它太冗长了。

那么,有什么简单的方法可以告诉上下文加载 Wiremock 吗?

只需为占位符添加一个默认值:

${wiremock.server.port:defaultValue}

最后我决定不尊重@Cwrwhaf,对不起..因为我不想对生产代码产生可能的负面影响; 我只想通过测试,任何更改都应仅限于测试本身的 scope。

所以我做了:

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

而且我知道这不是完美的答案,但与更改wiremock的全局设置相比影响较小。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM