[英]JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd)
[英]How to mock 3rd API response when 1st API and 2nd API is working on test environment
我有 3 個微服務想模擬最后一個 ms api 響應。
例如:我的第一個 api 發送了正確的請求。 並且在第二台 MS API 中使用了一些字段來將請求發送到第三台 MS API。
現在我想從第三個 MS api 發送 403。
在這種情況下,您可以使用Wiremock 。 這是一個用於存根和 mocking web 服務的庫。
這是基本用法的示例:
int port = 8080;
WireMockServer wireMockServer = new WireMockServer(port);
wireMockServer.start();
stubFor(get(urlEqualTo("/ms/api/v3"))
.willReturn(aResponse()
.withStatus(HttpStatus.FORBIDDEN.value())));
// after completing all your tests
wireMockServer.shutdown();
首先,你配置你的 WireMock 服務器。 然后為特定的 URL 指定存根。 完成所有工作后,您還應該關閉服務器。
如果您使用的是 Spring 框架和 JUnit,那么將 WireMock 服務器設置為 Spring-manged bean 可能是個好主意。 這將消除很多開銷。 請看下面我的配置示例:
import com.github.tomakehurst.wiremock.WireMockServer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
@TestConfiguration
public class WiremockConfig {
private static final Integer WIREMOCK_PORT = 8080;
@Bean(initMethod = "start", destroyMethod = "stop")
public WireMockServer wireMockServer() {
return new WireMockServer(WIREMOCK_PORT);
}
}
您可能需要的依賴項:
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.27.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.32.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.mkammerer.wiremock-junit5</groupId>
<artifactId>wiremock-junit5</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.