Micronaut set up EmbeddedServer for Pact test

☆樱花仙子☆ 提交于 2019-12-13 02:56:53

问题


I have this SpringBoot and Pact test example from Writing Contract Tests with Pact in 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");
  }

}

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);

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)

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?

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

Any ideas?


回答1:


You can use micronaut and pact with junit5, simple example based on hello-world-java:

Add pact dependencies to 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\"}");
    }
}


来源:https://stackoverflow.com/questions/56889600/micronaut-set-up-embeddedserver-for-pact-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!