You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WireMock存根问题:无法正确实现AssertThat方法

Troubleshooting WireMock Stub Issues with GET Assertions

It sounds like you’re hitting common pitfalls when setting up WireMock stubs and verifying GET requests—let’s break this down step by step to get your tests working smoothly.

First, looking at your code snippet, you’re using both WireMockRule and WireMockServer in the same test class. These two serve similar purposes but manage lifecycle differently, which can lead to conflicts. Pick one approach to avoid confusion:

  • WireMockRule is a JUnit Rule that automatically starts/stops the server before/after each test method (great for per-test isolation).
  • WireMockServer requires explicit start()/stop() calls (better if you want to reuse the server across all test methods).

Let’s walk through a clean, working example using WireMockRule since it handles lifecycle overhead for you:

Step 1: Correct Stub Setup & Assertion

First, define your mock GET endpoint, then use WireMock’s built-in verification methods to confirm the request was received:

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class WeatherApplicationTest {

    // Use WireMockRule with a fixed port (or random port with WireMockConfiguration.options().dynamicPort())
    @Rule
    public WireMockRule wireMockRule = new WireMockRule(8080);

    @Test
    public void testWeatherEndpointCall() {
        // 1. Setup the stub for your GET endpoint
        stubFor(get(urlEqualTo("/api/weather"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json")
                        .withBody("{\"temperature\": 22, \"condition\": \"cloudy\"}")));

        // 2. Make the GET request using your client (e.g., RestTemplate, OkHttp)
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://localhost:" + wireMockRule.port() + "/api/weather", String.class);

        // 3. Verify the request was sent to WireMock (this is the assertion you're struggling with)
        verify(getRequestedFor(urlEqualTo("/api/weather")));
        
        // Optional: Verify the response content if needed
        assertThat(response, containsString("cloudy"));
    }
}

Common Fixes for Your Issues

  • Port Mismatch: Ensure your client is targeting the same port WireMock uses. If you use a dynamic port, fetch it with wireMockRule.port() instead of hardcoding 8080.
  • URL Matching Errors: Double-check that the URL in your stub exactly matches what your client sends (trailing slashes, query parameters, and case sensitivity all matter). Use urlMatching() instead of urlEqualTo() if you need flexible regex matching.
  • Incorrect Assertion Method: WireMock tracks incoming requests internally—use verify(getRequestedFor(...)) to confirm your client made the expected call, rather than generic JUnit assertions that don’t check WireMock’s request log.
  • Lifecycle Conflicts: If you stick with WireMockServer, make sure to start/stop it explicitly in @BeforeClass and @AfterClass:
    public static WireMockServer wireMockServer = new WireMockServer();
    
    @BeforeClass
    public static void setUpClass() {
        wireMockServer.start();
    }
    
    @AfterClass
    public static void tearDownClass() {
        wireMockServer.stop();
    }
    

If you’re still seeing errors, share the specific exception messages or the exact assertion that’s failing—this will help narrow down the root cause even further!

内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:23:50