【发布时间】:2019-08-02 14:48:35
【问题描述】:
我正在使用 FeignClient 进行微服务之间的通信。我想测试一个微服务而不运行另一个,所以我需要以某种方式模拟它的响应。这时候我在嘲讽feignClient。但是,在这种情况下模拟 FeignClient 的响应是正确的方法吗?
我的 FeignClient:
@FeignClient(name="shortestPath", url="http://localhost:5000")
public interface GraphFeignClient {
@PostMapping(path="/short")
public List<Integer> getShortestPath(@RequestParam("source") int source,
@RequestParam("target") int target,
@RequestBody Graph graph);
}
我的测试:
@SpringBootTest
public class GraphJsonApplicationTests {
@Mock
GraphFeignClient graphFeignClient;
@Autowired
@InjectMocks
private GraphServiceClient graphServiceClient;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSavingShortestPath() throws Exception {
given(graphFeignClient.getShortestPath(anyInt(),anyInt(),any()))
.willReturn(Arrays.asList(1,2,3,4,5));
//...
}
}
【问题讨论】:
标签: spring-boot junit mockito spring-cloud-feign