Tag: 弹簧测试

如何使用Spring在unit testing中模拟远程REST API?

假设我在我的应用程序中创建了一个使用远程Web服务的简单客户端,该服务在某个URI /foo/bar/{baz}公开RESTful API。 现在我想对我的客户端进行unit testing,该客户端调用此Web服务。 理想情况下,在我的测试中,我想根据/foo/bar/123或/foo/bar/42等特定请求模拟我从Web服务获得的响应。 我的客户端假设API实际上在某处运行,所以我需要一个本地“Web服务”来开始在http://localhost:9090/foo/bar运行我的测试。 我希望我的unit testing是自包含的,类似于使用Spring MVC Test框架测试Spring控制器。 一些简单客户端的伪代码,从远程API获取数字: // Initialization logic involving setting up mocking of remote API at // http://localhost:9090/foo/bar @Autowired NumberClient numberClient // calls the API at http://localhost:9090/foo/bar @Test public void getNumber42() { onRequest(mockAPI.get(“/foo/bar/42”)).thenRespond(“{ \”number\” : 42 }”); assertEquals(42, numberClient.getNumber(42)); } // .. 使用Spring有哪些替代方案?