We have an API that returns the JSP as a view, for example:
@RequestMapping(value = "/cricket/{matchId}", method = RequestMethod.GET)
public String getCricketWebView(HttpServletRequest request, @PathVariable("matchId") Integer matchId, ModelMap mv){
try{
return "webforms/cricket";
}catch(Exception e){
e.printStackTrace();
}
return "";
}
I wrote unit test to test this as follows:
@Test
public void test_cricket()
{
try {
MvcResult result =this.mockMvc.perform(get(BASE + "/cricket/123")
.accept(MediaType.TEXT_HTML))
.andExpect(status().isOk()).andReturn();
String json = result.getResponse().getContentAsString();
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
}
The problem is that unit tests return a string webforms/cricket, not the actual HTML from the page cricket.jsp. I understand that this is happening because I use Mock MVC.
But is there a way to check the actual HTML? The reason is that we use some complex JSTL tags, and in the past we have seen that unit test succeeds, but the actual JSP page returns 500 errors due to parsing failure.
I tried the following code:
try {
WebConversation conversation = new WebConversation();
GetMethodWebRequest request = new GetMethodWebRequest(
"http://localhost:8080/cricket/123");
WebResponse response = conversation.getResponse(request);
System.out.println(response.getResponseMessage());
}
catch (Exception e)
{
e.printStackTrace();
org.junit.Assert.fail("500 error");
}
But this gives the connection a denied exception. Again, I understand that this is because the web server is not configured during the test.
This is my configuration:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/spring-resources/applicationcontext.xml")
public class MobileApiControllerTest {
...
}
@WebIntegrationTest, . , Spring. WAR, Tomcat.
, JSP unit test?