Micronaut设置EmbeddedServer进行Pact测试 [英] Micronaut set up EmbeddedServer for Pact test

查看:153
本文介绍了Micronaut设置EmbeddedServer进行Pact测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自的SpringBoot和Pact测试示例.在Spring Boot中使用契约编写合同测试

@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文件,我需要启动一个模拟提供程序,该提供程序设置为:

In order to generate the PACT file I need to start a mock Provider, which is set up as:

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

@SpringBootTest批注提供了在 http://localhost:8080

The @SpringBootTest annotation provides a mock web environment running on http://localhost:8080

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

是否可以在Micronaut中做类似的事情?我可以使用在指定端口(例如 http://localhost:8080 )中运行的EmbeddedServer以便我的Pact MockProvider可以侦听那个端口?

Is it possible to do something similar in Micronaut? Can I use an EmbeddedServer running in a specified port such as http://localhost:8080 so my Pact MockProvider can listen to that port?

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

I would like to specify the port in the test class, not into an application.yml file

有什么想法吗?

推荐答案

您可以将micronautpactjunit5结合使用,这是基于

You can use micronaut and pact with junit5, simple example based on 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\"}");
    }
}

这篇关于Micronaut设置EmbeddedServer进行Pact测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆