【问题标题】:Spring boot integration test error: "Could not resolve placeholder 'wiremock.server.port'" in tests not requiring WiremockSpring Boot 集成测试错误:“无法解析占位符 'wiremock.server.port'”在不需要 Wiremock 的测试中
【发布时间】:2020-02-18 20:44:26
【问题描述】:

我有一个@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:无法解析占位符 'wiremock.server.port' 的值 "http://localhost:${wiremock.server.port}/send"

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

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

有问题的测试:

@SpringBootTest
public class MapperLookupTest {
    ...
}

Wiremock 依赖已经有compile 范围:

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

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

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

【问题讨论】:

    标签: java spring-boot spring-boot-test wiremock


    【解决方案1】:

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

    所以我做到了:

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

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

    【讨论】:

      【解决方案2】:

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

      ${wiremock.server.port:defaultValue}
      

      【讨论】:

      • 啊,你的意思是在 yaml 文件中。好的,这是我不知道的。谢谢。最后我用@SpringBootTest(properties = "wiremock.server.port=8080"),大同小异。
      猜你喜欢
      • 2016-01-20
      • 1970-01-01
      • 2016-02-20
      • 2019-11-10
      • 2017-11-16
      • 2019-07-30
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      相关资源
      最近更新 更多