我有一个来自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)中运行的EmbeddedServer,以便我的Pact MockProvider可以侦听该端口?
我想在测试类中指定端口,而不是在application.yml文件中
有任何想法吗?
最佳答案
您可以将micronaut
和pact
与junit5
一起使用,这是基于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/