首页 > 解决方案 > Spring Boot 集成测试错误:“无法解析占位符 'wiremock.server.port'”在不需要 Wiremock 的测试中

问题描述

我有一个测试 a及其方法@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作用域:

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

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

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

标签: javaspring-bootspring-boot-testwiremock

解决方案


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

${wiremock.server.port:defaultValue}

推荐阅读