线模拟-Spring云模拟集成测试

uklbhaso  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(45)

我有一个REST控制器,它与一个服务类进行对话,该服务类调用feign client。spring应用程序启动正常,工作正常。现在我正在尝试使用wire mock为spring feign client编写集成测试;通过遵循here的指南。当运行测试时,我看到Bean Currently in Creation异常。有人能告诉我我错过了什么吗?
下面是wire mock config类:

@TestConfiguration
@ActiveProfiles("test")
public class WireMockConfig {
    @Autowired
    private WireMockServer wireMockServer;

    @Bean(initMethod = "start", destroyMethod = "stop")
    public WireMockServer mockServer(){
        return new WireMockServer(options().dynamicPort());
    }
}

字符串
我将响应截短为如下所示:

public class DataMocks {
    public static void setUpTypesResponse(WireMockServer mockService) throws Exception{
        mockService.stubFor(WireMock.get(WireMock.urlEqualTo("/endpoint"))
                .willReturn(WireMock.aResponse()
                        .withStatus(HttpStatus.OK.value())
                        .withHeader(Header.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                        .withBody(
                                copyToString(
                                        DataMocks.class.getClassLoader().getResourceAsStream("payload/Response.json"),
                                        Charset.defaultCharset()))));
    }


下面是我的集成测试类:

@SpringBootTest
@ActiveProfiles("test")
@EnableConfigurationProperties
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {WireMockConfig.class})
class DataFeignClientIntegrationTest{
    @Autowired
    private WireMockServer mockServer;

    @Autowired
    private FeignClient feignClient;

    @BeforeEach
    void setup() throws Exception {
        DataMocks.setUpTypesResponse(mockServer);
    }

    @Test
    public void findTypesTest() throws Exception{
        RequestParams requestParams = RequestParams.builder().build();
        assertEquals(feignClient.findTypes(requestParams).getClass(),Response.class);
    }


错误是:
创建名为“wireMockConfig”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?

6jjcrrmo

6jjcrrmo1#

为什么要尝试在Config类中自动连接WireMockServer?
config类是您的WireMockServer将要使用的类。
删除以下行:

@Autowired
private WireMockServer wireMockServer;

字符串
和测试

相关问题