spring与wiremock和eureka的boot集成测试失败,并显示“无可用示例”

7nbnzgx9  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(422)

当为使用restemplate(以及引擎盖下的ribbon)和eureka来解决服务b依赖关系的spring引导应用程序(servicea)编写集成测试时,我在调用servicea时遇到了一个“no instances available”异常。
我试图通过wiremock模拟服务b,但我甚至没有访问wiremock服务器。restemplate似乎试图从eureka获取服务示例,而eureka在我的测试中没有运行。它是通过属性禁用的。
服务a呼叫服务b。服务发现是通过restemplate、ribbon和eureka完成的。
有人有一个包括spring、eureka和wiremock的工作示例吗?

2o7dmzc5

2o7dmzc51#

这就是我在项目中所做的:
在项目配置中的某个位置:

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

和测试类 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服务器。所以我在test application.properties中添加了eureka.client.enabled=false

gupuwyp2

gupuwyp22#

我昨天也遇到了同样的问题,为了完整起见,这里是我的解决方案:
这是我的“现场”配置下 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();
    }
}

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

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

在测试用例中,我激活了profile 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
   }
}
q0qdq0h2

q0qdq0h23#

希望这能帮助别人。我在ribbon上也犯了同样的错误,但没有eureka。
帮助我的是
1) 升级到wiremock(2.21)的最新版本
2) 为url“/”添加wiremock规则存根以回答ribbon的ping

相关问题