簡體   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