WireMock和Eureka的Spring Boot集成测试失败,“没有实例可用”

问题描述 投票:4回答:3

在为使用RestTemplate(和引擎盖下的功能区)和Eureka来解析服务B依赖关系的Spring Boot应用程序(服务A)编写集成测试时,在调用服务A时出现“无实例可用”异常。

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

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

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

java spring spring-boot netflix-eureka wiremock
3个回答
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
   }
}

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


0
投票

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

是什么帮助了我

1)在我的情况下升级到最新版本的WireMock(2.21)

2)为url“/”添加wiremock规则存根以回答Ribbon的ping

© www.soinside.com 2019 - 2024. All rights reserved.