It's possible to mock response FeignClient via MockRestServiceServer(restTemplate)? This example dosn't work:
Application.class
@SpringBootApplication
@EnableFeignClients
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
TicketService.class
@FeignClient("ws")
public interface TicketService {
    @RequestMapping(value = "/tickets/")
    List<Ticket> findAllTickets();
}
TestConfig.class
@Profile("test")
@Configuration
public class TestConfig {
    @Bean
    @Primary
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
MyTest.class
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class}, properties = {"ws.ribbon.listOfServers:example.com"})
public class MyTest {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DispatcherService dispatcherService; // service where the execution of the method TicketService.findAllTickets();
    private MockRestServiceServer mockServer;
    @Before
    public void setUp() {
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }
    @Test
    public void ticket() {
        mockServer.expect(requestTo("http://example.com/tickets/"))
                .andExpect(method(HttpMethod.GET))
                .andRespond(withSuccess(new ClassPathResource("tickets.json"), MediaType.APPLICATION_JSON));
        dispatcherService.run();
    }
}
But going a request to the real server example.com.
At the moment I know 2 good approaches:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With