Wiremock 与 spring boot 的集成抛出 404 异常,因为“请求不匹配,因为存根未注册”

问题描述 投票:0回答:0

我一直在使用 wiremock 与 spring boot 进行集成测试,得到 404 异常。

消息为“请求不匹配,因为没有注册存根”

我已经尝试过 wiremock 服务器和 wiremock 规则实例。

尝试使用 Wiremock 的 JUnit jupiter 扩展方法和应用程序上下文初始化方法,似乎没有任何效果。

package fi.op.bcsio.application.wiremock;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import java.util.function.Supplier;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

import javax.annotation.PostConstruct;

import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.client.RestTemplate;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;

import org.assertj.core.api.Assertions;

@Component
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@ContextConfiguration(initializers = {WireMockInitializer.class})
public class LuukuWiremockStub {

//    @Autowired
//    private WebTestClient webTestClient;
    
    
//  @ClassRule
//  public static WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.DYNAMIC_PORT);

      @RegisterExtension
      static WireMockExtension wireMockServer = WireMockExtension.newInstance()
        .options(WireMockConfiguration.options().dynamicPort())
        .build();

//    @DynamicPropertySource
//    static void configureProperties(DynamicPropertyRegistry registry) {
//      registry.add("http://op-common.apis.jty.op-palvelut.net/bankingparties/basic/info/v1/parties?context=Pankin ostopolku", wireMockServer::baseUrl);
//    }

//    @AfterEach
//    void resetAll() {
//      // we're using one WireMock server for the test class (see static on the WireMockExtension definition)
//        wireMockRule.resetAll();
//    }

      @Test
      void testGetAllTodosShouldReturnDataFromClient() {
          wireMockServer.stubFor(
          WireMock.get("/parties")
          
          .withHeader("x-request-id", matching("[0-9a-fA-F]{8}\\\\-[0-9a-fA-F]{4}\\\\-[0-9a-fA-F]{4}\\\\-[0-9a-fA-F]{4}\\\\-[0-9a-fA-F]{12}"))
          
            .withHeader("x-session-id", matching("[0-9a-fA-F]{8}\\\\-[0-9a-fA-F]{4}\\\\-[0-9a-fA-F]{4}\\\\-[0-9a-fA-F]{4}\\\\-[0-9a-fA-F]{12}"))
                      
            .withHeader("x-api-key", equalTo("V7Qx9EAVpIsvAjrMsxibCucSM0Kyu3Mi"))
                      
            .withHeader("Authorization", matching("Bearer [a-zA-Z0-9&._-]{1,}"))
            
            .withQueryParam("context", equalTo("Pankin ostopolku"))
          
            .willReturn(aResponse()
            
            .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
              
            .withBody("[{\"userId\": 1,\"id\": 1,\"title\": \"Learn Spring Boot 3.0\", \"completed\": false}," +
                "{\"userId\": 1,\"id\": 2,\"title\": \"Learn WireMock\", \"completed\": true}]"))
        );
        
        // ... controller invocation using the WebTestClient
      }
}

Postman console errorEclipse Console error

java spring-boot integration-testing wiremock
© www.soinside.com 2019 - 2024. All rights reserved.