使用WireMock和Eureka的Spring Boot集成测试失败,并且#34;没有可用实例"

时间:2017-09-04 19:14:51

标签: java spring spring-boot netflix-eureka wiremock

为Spring Boot应用程序(服务A)编写集成测试时,使用RestTemplate(以及功能区内的功能区)和Eureka来解析服务B依赖关系,我得到了一个"没有可用的实例"调用服务A时的异常。

我尝试通过WireMock模拟服务B,但我甚至都没有去过WireMock服务器。似乎RestTemplate尝试从Eureka获取Service实例,而Eureka在我的测试中没有运行。它通过属性禁用。

服务A呼叫服务B. 服务发现通过RestTemplate,Ribbon和Eureka完成。

有没有人有一个包含Spring,Eureka和WireMock的工作示例?

3 个答案:

答案 0 :(得分:1)

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

这是src/main/java/.../config下的“实时”配置:

//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
   }
}

答案 1 :(得分:0)

这就是我在项目中所做的:

项目配置中的某处:

@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;
}

和SomeRestClass:

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
    }
}

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

答案 2 :(得分:0)

希望这可以对某人有所帮助。 Ribbon出现了相同的错误,但没有Eureka。

对我有帮助的是

1)就我而言,升级到最新版本的WireMock(2.21)

2)为网址“ /”添加一个Wiremock规则存根以回答功能区的ping