我正在尝试编写一个测试辅助类中的方法的JUnit测试用例。该方法使用REST调用外部应用程序,这是我试图在JUnit测试中模拟的这个调用。用MockRestServiceServer嘲笑REST调用
辅助方法使用Spring的RestTemplate进行REST调用。
在我的测试中,我创建了一个模拟REST服务器和模拟REST模板和instanitiate他们是这样的:
@Before
public void setUp() throws Exception {
mockServer = MockRestServiceServer.createServer(helperClass.getRestTemplate());
}
我再播种模拟服务器,以便当辅助方法使得它应该返回一个适当的反应REST调用:
// response is some XML in a String
mockServer
.expect(MockRestRequestMatchers.requestTo(new URI(myURL)))
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_XML)
.body(response));
当我运行我的测试时,helper方法收到来自REST调用的空响应并且测试失败。
其余网址助手造就了查询参数,看起来像这样:“http://server:port/application/resource?queryparam1=value1&queryparam2=value2”。
我试图把URL(“http://server:port/application/resource”)都与和无查询参数在‘myURL’变量(引起的匹配,使其返回响应),但不能得到模拟服务器返回任何东西。
我试着寻找这种代码的例子,但还没有找到任何东西,这似乎像我的方案。
春季版4.1.7。
在此先感谢您的帮助。
Tha用于将该类写入静态方法的不同之处在于它不像其他许多源一样。 'requestTo'。 – sjngm