Simple library for testing Feign Clients using MockMvc or TestRestTemplate instances.
Useful for when you want to test your Spring REST Controllers and associated Feign Client for compatibility.
See the src/test/java
folder for a fully fledged example, but in short:
- Include this library as a test dependency
- Add Feign global configuration (to use
MockMvc
orTestRestTemplate
based upon your preference)- MockMvc should execute quicker as TestRestTemplate requires a full server stack
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@EnableFeignClients(defaultConfiguration = MockMvcFeignConfiguration.class)
public class MockMvcFeignClientTest {
// ...
}
public class MockMvcFeignConfiguration {
@Bean
Client feignClient() {
return new MockMvcFeignClient();
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@EnableFeignClients(defaultConfiguration = TestRestTemplateFeignConfiguration.class)
public class RestTemplateFeignClientTest {
// ...
}
public class TestRestTemplateFeignConfiguration {
@Bean
Client feignClient() {
return new RestTemplateFeignClient();
}
}