Giriş
1. FakeFeignConfiguration yaratılır. Bu sınıf içinde FakeRibbonConfiguration ayağa kaldırılır. 2. FakeRibbonConfiguration da çağrıları @LocalServerPort ile testin kullandığı porta yönlendirir.
3. Test içinde FakeMessagingRestService ayağa kaldırılır
Örnek
Açıklaması şöyle
Spring uses OpenFeign under the hood to build the client component that does the rest-based service call and uses Netflix Ribbon to provide client-side load balancing to the provisioned client component.
Örnek
Elimizde FeignClient kodu olsun
@FeignClient(name = "messagingRestClient", path = "/messaging")
public interface MessagingRestClient {
@GetMapping(params = {"name"})
Message getMessage(@RequestParam("name") final String name);
@PostMapping(params = {"name"})
Message setMessage(@RequestParam("name") final String name,
@RequestBody final Message message);
}
@SpringBootTest(classes = {MessagingRestClientBootTests.FakeFeignConfiguration.class,
MessagingRestClientBootTests.FakeMessagingRestService.class},
webEnvironment = WebEnvironment.RANDOM_PORT)
class MessagingRestClientBootTests {
@RestController
@RequestMapping(path = "/messaging")
static class FakeMessagingRestService {
@GetMapping(params = {"name"},produces = "application/json")
public String getMessage(@RequestParam("name") final String name) {
assertThat(name).isEqualTo("Foo");
return "{\"text\":\"Hello, Foo\"}";
}
@PostMapping(params = {"name"}, produces = "application/json")
public String setMessage(@RequestParam("name") final String name,
@RequestBody final String message) throws Exception {
assertThat(name).isEqualTo("Foo");
JSONAssert.assertEquals(message, "{ \"text\":\"Hello\" }", false);
return "{\"text\":\"Hi, Foo\"}";
}
}
@Configuration(proxyBeanMethods = false)
static class FakeRibbonConfiguration {
@LocalServerPort int port;
@Bean
public ServerList<Server> serverList() {
return new StaticServerList<>(new Server("localhost", port));
}
}
@Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = MessagingRestClient.class)
@EnableAutoConfiguration
@RibbonClient(name = "messagingRestClient",
configuration = MessagingRestClientBootTests.FakeRibbonConfiguration.class)
static class FakeFeignConfiguration {}
}
Aynı sınıfta testleri şöyle yaparız
@SpringBootTest(
classes = {
MessagingRestClientBootTests.FakeFeignConfiguration.class,
MessagingRestClientBootTests.FakeMessagingRestService.class
},
webEnvironment = WebEnvironment.RANDOM_PORT)
class MessagingRestClientBootTests {
@Autowired MessagingRestClient client;
@Test
public void getMessage() {
final Message response = client.getMessage("Foo");
assertThat(response.getText()).isEqualTo("Hello, Foo");
}
@Test
public void setMessage() {
final Message message = new Message();
message.setText("Hello");
final Message response = client.setMessage("Foo", message);
assertThat(response.getText()).isEqualTo("Hi, Foo");
}
...
}
Örnek
Elimizde FeignClient kodu olsun
@FeignClient(name = "test")
public interface FeignAPI {
@RequestMapping(value = "hello")
String hello();
}
Şöyle yaparız. Burada ilk örnekten farklı olarak RestController ayrı bir sınıf değil, FeignConfig içinde tanımlı. Aslında aynı kapıya çıkıyor.
@SpringBootTest(classes = FeignAPITest.FeignConfig.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FeignAPITest {
@Autowired
FeignAPI feignAPI;
@Configuration
static class RibbonConfig {
@LocalServerPort
int port;
@Bean
public ServerList<Server> serverList() {
return new StaticServerList<>(new Server("127.0.0.1", port));
}
}
@EnableFeignClients(clients = FeignAPI.class)
@RestController
@Configuration
@EnableAutoConfiguration
@RibbonClient(name = "test", configuration = FeignAPITest.RibbonConfig.class)
static class FeignConfig {
@RequestMapping(value = "hello")
public String testFeign() {
return "success";
}
}
@Test
public void testFeign() {
assertThat(this.feignAPI.hello()).isEqualTo("success");
}
}