Spring 使用 WireMock 和 Eureka 的启动集成测试失败并显示 "No instances available"

Spring Boot Integration Test with WireMock and Eureka fails with "No instances available"

在为使用 RestTemplate(和引擎盖下的功能区)和 Eureka 解决服务 B 依赖关系的 Spring 启动应用程序(服务 A)编写集成测试时,我得到 "No instances available"调用服务A时出现异常

我尝试通过 WireMock 模拟服务 B,但我什至没有访问 WireMock 服务器。似乎 RestTemplate 试图从 Eureka 获取服务实例,这在我的测试中没有 运行。它通过属性禁用。

服务 A 调用服务 B。 服务发现是通过 RestTemplate、Ribbon 和 Eureka 完成的。

有人有包含 Spring、Eureka 和 WireMock 的工作示例吗?

这是我在项目中所做的:

项目配置中的某处:

@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    RestTemplate restTemplate = builder.build();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    return restTemplate;
}

@Bean
public SomeRestClass someRestClass () {
    SomeRestClass someRestClass = new SomeRestClass (environment.getProperty("someservice.uri"), restTemplate(new RestTemplateBuilder()));
    return parameterRest;
}

还有一些休息类:

public class SomeRestClass {

    private final RestTemplate restTemplate;

    private final String someServiceUri;

    public LocationRest(String someServiceUri, RestTemplate restTemplate) {
        this.someServiceUri= someServiceUri;
        this.restTemplate = restTemplate;
    }

    public String getSomeServiceUri() {
        return locationUri;
    }

    public SomeObject getSomeObjectViaRest() {
        //making rest service call
    }
}

并测试 class SomeRestClass:

@RunWith(SpringRunner.class)
@RestClientTest(SomeRestClass.class)
public class SomeRestClassTest {
    @Autowired
    private SomeRestClass someRestClass;

    @Autowired
    private MockRestServiceServer server;

    @Test
    public void getSomeObjectViaRestTest() throws JsonProcessingException {
        SomeResponseObject resObject = new SomeResponseObject();
        ObjectMapper objectMapper = new ObjectMapper();
        String responseString = objectMapper.writeValueAsString(resObject);

        server.expect(requestTo(locationRest.getSomeServiceUri() + "/some-end-point?someParam=someParam")).andExpect(method(HttpMethod.POST))
            .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON_UTF8).body(responseString));
        someRestClass.getSomeObjectViaRest();

    }
}

注意:我禁用了 eureka 客户端,否则你必须模拟 eureka 服务器。所以我在测试 application.properties

中添加了 eureka.client.enabled=false

我昨天遇到了同样的问题,为了完整起见,这里是我的解决方案:

这是我在src/main/java/.../config下的"live"配置:

//the regular configuration not active with test profile
@Configuration
@Profile("!test")
public class WebConfig {
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        //you can use your regular rest template here.
        //This one adds a X-TRACE-ID header from the MDC to the call.
        return TraceableRestTemplate.create();
    }
}

我将此配置添加到测试文件夹src/main/test/java/.../config:

//the test configuration
@Configuration
@Profile("test")
public class WebConfig {
    @Bean
    RestTemplate restTemplate() {
        return TraceableRestTemplate.create();
    }
}

在测试用例中,我激活了配置文件 test:

 //...
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ServerCallTest {
   @Autowired
   private IBusiness biz;

   @Autowired
   private RestTemplate restTemplate;

   private ClientHttpRequestFactory originalClientHttpRequestFactory;

   @Before
   public void setUp() {
       originalClientHttpRequestFactory = restTemplate.getRequestFactory();
   }

   @After
   public void tearDown() {
      restTemplate.setRequestFactory(originalClientHttpRequestFactory);
   }

   @Test
   public void fetchAllEntries() throws BookListException {
      MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);

       mockServer                
            .andExpect(method(HttpMethod.GET))
            .andExpect(header("Accept", "application/json"))
            .andExpect(requestTo(endsWith("/list/entries/")))
            .andRespond(withSuccess("your-payload-here", MediaType.APPLICATION_JSON));

       MyData data = biz.getData();

       //do your asserts
   }
}

希望这可以帮助某人。我在使用 Ribbon 时遇到了同样的错误,但没有使用 Eureka。

对我有帮助的是

1) 在我的案例中升级到最新版本的 WireMock (2.21)

2) 为 url "/" 添加一个 wiremock 规则存根以回答 Ribbon 的 ping