Although below test works fine in one enviroment how can the test be modified so that it does not depend on what server the
getJson() service is running on ?
Do I need to create a mock Spring json object so that it does not matter what the domain name is (in this case its 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}";
}
}
You can use spring-test-mvc. Or if you don’t want an additional dependency, you can write a unit test calling the controller directly, as opposed to through an URL.