尽管下面的测试在一个环境中运行良好,但如何修改测试以使其不依赖于 getJson() 服务在哪个服务器上运行?
我是否需要创建一个模拟 Spring json 对象,以便域名是什么(在本例中为它)无关紧要http://myapplication
?
@Test
public void jsonResponse() throws Exception
{
URL oracle = new URL("http://myapplication/newJson.json");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String json;
while ((inputLine = in.readLine()) != null)
json += inputLine;
in.close();
}
My controller class is defined like :
@Controller
@RequestMapping("myservice")
public class Controller {
@RequestMapping(value = "/newJson.json", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String getJson() {
return "{ test:testJson}";
}
}