如何使用spring-cloud-netflix和假装编写集成测试
2022-09-01 16:04:10
我使用Spring-Cloud-Netflix进行微服务之间的通信。假设我有两个服务,Foo 和 Bar,Foo 使用 Bar 的一个 REST 端点。我使用一个注释为:@FeignClient
@FeignClient
public interface BarClient {
@RequestMapping(value = "/some/url", method = "POST")
void bazzle(@RequestBody BazzleRequest);
}
然后我在Foo中有一个服务类,它调用.SomeService
BarClient
@Component
public class SomeService {
@Autowired
BarClient barClient;
public String doSomething() {
try {
barClient.bazzle(new BazzleRequest(...));
return "so bazzle my eyes dazzle";
} catch(FeignException e) {
return "Not bazzle today!";
}
}
}
现在,为了确保服务之间的通信正常工作,我想构建一个测试,使用类似WireMock的东西,对假的Bar服务器发出真正的HTTP请求。测试应确保假装正确解码服务响应并将其报告给 。SomeService
public class SomeServiceIntegrationTest {
@Autowired SomeService someService;
@Test
public void shouldSucceed() {
stubFor(get(urlEqualTo("/some/url"))
.willReturn(aResponse()
.withStatus(204);
String result = someService.doSomething();
assertThat(result, is("so bazzle my eyes dazzle"));
}
@Test
public void shouldFail() {
stubFor(get(urlEqualTo("/some/url"))
.willReturn(aResponse()
.withStatus(404);
String result = someService.doSomething();
assertThat(result, is("Not bazzle today!"));
}
}
如何将这样的WireMock服务器注入eureka,以便假装能够找到它并与之通信?我需要什么样的注释魔法?