Offline test of Servlets

Posted on by Kim

Easy way to test Servlest is to use springs mocks.

Eg.


@Test
public void testService() throws Exception {
System.out.println("service");

MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();

XmlServiceServlet servlet = new XmlServiceServlet();

new XmlServiceServlet().doGet(request, response);

System.out.println(response.getContentAsString());
}


Just at folling to the pom

<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>2.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-mock</artifactid>
<version>2.0-rc3</version>
<scope>test</scope>
</dependency>

See: Article

0 Responses to "Offline test of Servlets":