1 回答

TA貢獻2051條經驗 獲得超10個贊
我想“任何”實際上是一個 Mockito.any() 方法?在這種情況下,您可以使用 Mockito.matches("regex")。請參閱文檔:https://static.javadoc.io/org.mockito/mockito-core/1.9.5/org/mockito/Matchers.html#matches(java.lang.String)
編輯:事實證明,MockRestServiceServer 使用 Hamcrest 匹配器來驗證期望(requestTo、withSuccess 等方法)。
org/hamcrest/Matchers類中還有一個方法matchesPattern(java.util.regex.Pattern pattern),自 Hamcrest 2 起可用,它可用于解決您的問題。
但是在您的項目中,您可能依賴于較舊版本的 Hamcrest (1.3),例如 junit 4.12、最新的 spring-boot-starter-test-2.13 或最后的 org.mock-server .mockserver-netty.3.10.8(傳遞)。
因此,您需要:
檢查項目中 Hamcrest 的實際版本并(如果不是 2+)手動更新此依賴項:https ://mvnrepository.com/artifact/org.hamcrest/hamcrest/2.1
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
更新您的測試:
mockServer.expect(requestTo(matchesPattern(".*exact-example-url.com.*")))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("response", MediaType.APPLICATION_JSON));
添加回答
舉報