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

uklbhaso  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(127)

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

  1. @TestConfiguration
  2. @ActiveProfiles("test")
  3. public class WireMockConfig {
  4. @Autowired
  5. private WireMockServer wireMockServer;
  6. @Bean(initMethod = "start", destroyMethod = "stop")
  7. public WireMockServer mockServer(){
  8. return new WireMockServer(options().dynamicPort());
  9. }
  10. }

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

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


下面是我的集成测试类:

  1. @SpringBootTest
  2. @ActiveProfiles("test")
  3. @EnableConfigurationProperties
  4. @ExtendWith(SpringExtension.class)
  5. @ContextConfiguration(classes = {WireMockConfig.class})
  6. class DataFeignClientIntegrationTest{
  7. @Autowired
  8. private WireMockServer mockServer;
  9. @Autowired
  10. private FeignClient feignClient;
  11. @BeforeEach
  12. void setup() throws Exception {
  13. DataMocks.setUpTypesResponse(mockServer);
  14. }
  15. @Test
  16. public void findTypesTest() throws Exception{
  17. RequestParams requestParams = RequestParams.builder().build();
  18. assertEquals(feignClient.findTypes(requestParams).getClass(),Response.class);
  19. }


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

6jjcrrmo

6jjcrrmo1#

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

  1. @Autowired
  2. private WireMockServer wireMockServer;

字符串
和测试

相关问题