gpt4 book ai didi

java - Micronaut 设置 EmbeddedServer 进行 Pact 测试

转载 作者:行者123 更新时间:2023-12-02 09:43:10 24 4
gpt4 key购买 nike

我有来自 Writing Contract Tests with Pact in Spring Boot 的 SpringBoot 和 Pact 测试示例

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = "user-service.base-url:http://localhost:8080",
classes = UserServiceClient.class)
public class UserServiceContractTest {

@Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2("user-service", null,
8080, this);

@Autowired
private UserServiceClient userServiceClient;

@Pact(consumer = "messaging-app")
public RequestResponsePact pactUserExists(PactDslWithProvider builder) {
return builder.given("User 1 exists")
.uponReceiving("A request to /users/1")
.path("/users/1")
.method("GET")
.willRespondWith()
.status(200)
.body(LambdaDsl.newJsonBody((o) -> o
.stringType("name", “user name for CDC”)
).build())
.toPact();
}

@PactVerification(fragment = "pactUserExists")
@Test
public void userExists() {
final User user = userServiceClient.getUser("1");
assertThat(user.getName()).isEqualTo("user name for CDC");
}

}

为了生成 PACT 文件,我需要启动一个模拟提供程序,其设置为:

public PactProviderRuleMk2 provider = new PactProviderRuleMk2("user-service", null,     
8080, this);

@SpringBootTest 注解提供了运行在 http://localhost:8080 上的模拟 Web 环境

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = "user-service.base-url:http://localhost:8080",
classes = UserServiceClient.class)

是否可以在 Micronaut 中做类似的事情?我可以使用在指定端口(例如http://localhost:8080)运行的嵌入式服务器吗?那么我的 Pact MockProvider 可以监听该端口吗?

我想在测试类中指定端口,而不是在 application.yml 文件中指定端口

有什么想法吗?

最佳答案

您可以将 micronautpactjunit5 一起使用,基于 hello-world-java 的简单示例:

pact 依赖项添加到 build.gradle:

    // pact
compile 'au.com.dius:pact-jvm-consumer-junit5_2.12:3.6.10'
compile 'au.com.dius:pact-jvm-provider-junit5_2.12:3.6.10'
// client for target example
compile 'io.micronaut:micronaut-http-client'

FooService.java:

import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;

import javax.inject.Inject;
import javax.inject.Singleton;

import static io.micronaut.http.HttpRequest.GET;

@Singleton
public class FooService {

@Inject
@Client("http://localhost:8080")
private RxHttpClient httpClient;

public String getFoo() {
return httpClient.retrieve(GET("/foo")).blockingFirst();
}

}

FooServiceTest.java:

import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.model.RequestResponsePact;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.Test;
import javax.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "foo", hostInterface = "localhost", port = "8080")
public class FooServiceTest {

@Inject
FooService fooService;

@Pact(provider = "foo", consumer = "foo")
public RequestResponsePact pact(PactDslWithProvider builder) {
return builder
.given("test foo")
.uponReceiving("test foo")
.path("/foo")
.method("GET")
.willRespondWith()
.status(200)
.body("{\"foo\":\"bar\"}")
.toPact();
}

@Test
public void testFoo() {
assertEquals(fooService.getFoo(), "{\"foo\":\"bar\"}");
}
}

关于java - Micronaut 设置 EmbeddedServer 进行 Pact 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56889600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com