/spring-mockmvc-feignclient

Feign Client implementation for use with Spring Mock Mvc / TestRestTemplate

Primary LanguageJavaApache License 2.0Apache-2.0

Spring Mock Feign Clients

Build Status Maven Central Coverage Status

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.

Usage

See the src/test/java folder for a fully fledged example, but in short:

  1. Include this library as a test dependency
  2. Add Feign global configuration (to use MockMvc or TestRestTemplate based upon your preference)
    • MockMvc should execute quicker as TestRestTemplate requires a full server stack

MockMvc Usage

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@EnableFeignClients(defaultConfiguration = MockMvcFeignConfiguration.class)
public class MockMvcFeignClientTest {
    // ...
}

public class MockMvcFeignConfiguration {
    @Bean
    Client feignClient() {
        return new MockMvcFeignClient();
    }
}

TestRestTemplate Usage

@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();
    }
}