2
MockRestServiceServer(restTemplate)経由でレスポンスFeignClientをモックすることは可能ですか? この例では、作業dosn't:TicketService.classMock FeignClientレスポンス
Application.class
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
を
@FeignClient("ws")
public interface TicketService {
@RequestMapping(value = "/tickets/")
List<Ticket> findAllTickets();
}
TestConfig.class
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();
}
}
しかし、本当のサーバexample.comに要求を行きます。私は2つの良いアプローチを知っている瞬間
ファインクライアントはインターフェイスです。最良の方法は、一般にインタフェースを直接模擬することです。 – chrylis