Launch a stub server with WireMock

WireMock's stub server is an API that causes HTTP communication to mock the destination port and return the expected return value.

--Environment - Java1.8 --Spring Boot 2 series --WireMock 2 series

If you make one


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FunctionalTest {

  @ClassRule
  public static WireMockRule wireMockRule =
    new WireMockRule(wireMockConfig().port(8081)
      .usingFilesUnderDirectory("src/functionalTest/resources/"));

  @Test
public void normal system() {
    stubFor(get(urlEqualTo("/hoge"))
      .willReturn(aResponse()
        .withStatus(200)
        .withBodyFile("hoge.json")))

    //Test content...

  }
}

hoge.json


{
  "text": "hogehoge"
}

Briefly, a stub server that returns 200 status and hogehoge when accessing / hoge will start up on port 8081. Note the location where hoge.json is placed. src/functionalTest/resources/__files/hoge.json
is. I forgot this __files and got hooked for an hour.

When making more than one

I will put the code first.


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FunctionalTest {

  @Rule
  public WireMockRule wireMockRuleHoge =
    new WireMockRule(wireMockConfig().port(8081)
      .usingFilesUnderDirectory("src/functionalTest/resources/"));

  @Rule
  public WireMockRule wireMockRuleFuga =
    new WireMockRule(wireMockConfig().port(8082)
      .usingFilesUnderDirectory("src/functionalTest/resources/"));

  @Test
public void normal system() {

    wireMockRuleHoge.stubFor(get(urlEqualTo("/hoge"))
      .willReturn(aResponse()
        .withStatus(200)
        .withBodyFile("hoge.json")
      )
    );

    Resource requestResource = resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + "/fuga.json");
    String request = FileUtils.readFileToString(requestResourcePerformance.getFile(), UTF_8);  
    wireMockRuleFuga.stubFor(post(urlEqualTo("/fuga"))
      .withRequestBody(equalToJson())
      .willReturn(ok())
    );

    //Test content...

  }
}

As you can see, if you send fugafuga to / fuga by post, you can add a stub that returns only the 200th status. As an advantage, verify etc. are prepared for each rule, so you can test what POST request of which stub was called and how many times.

Recommended Posts

Launch a stub server with WireMock
Write a Reactive server with Micronaut
Prepare a LEMP Ubuntu 18 server with Ansible Galaxy
Create a web api server with spring boot
Set up a CentOS virtual server with Vagrant
Play down with Raspberry PI4 as a server. Part 2
Launch MariaDB with Docker
Implementing a large-scale GraphQL server in Java with Netflix DGS
[Rails] Launch a new project
Launch a terminal on VScode
Create a playground with Xcode 12
Draw a gradient with CAGradientLayer
A story stuck with NotSerializableException
Implement a simple Web REST API server with Spring Boot + MySQL
Create a simple web server with the Java standard library com.sun.net.httpserver
A story of connecting to a CentOS 8 server with an old Ansible