Micronaut设置EmbeddedServer进行Pact测试

时间:2019-07-04 14:00:42

标签: java spring-boot junit pact micronaut

我有一个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文件中指定

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以将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\"}");
    }
}