Reputation: 1217
I am using com.meterware.servletunit.ServletRunner to initialize one of my servlets. I try to use the next method:
ServletRunner.registerServlet(String resourceName, String servletClassName)
The resourceName, as I understand, is servlet mapping, say "/myservlet/*" or so.
But the problem is this servlet has no mapping in web.xml file and is supposed to be initialized on startup. I need to initialize this servlet in my JUnit. How can I do that?
Upvotes: 1
Views: 88
Reputation: 310
you can simply do the following and it should work as expected where HelloAppEngine is my servlet class
@Test
public void doGetWritesResponse() throws Exception {
ServletRunner sr = new ServletRunner();
sr.registerServlet("/hello", HelloAppEngine.class.getName());
WebClient wc = sr.newClient();
WebResponse response = wc.getResponse(http://localhost/hello");
assertEquals("content type", "text/plain", response.getContentType());
assertEquals("response text is ", "Hello app Engine", response.getText());
}
the source code for HelloAppEngine
is as the following
@WebServlet(name = "HelloAppEngine", value = "/hello")
public class HelloAppEngine extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException {
response.setContentType("text/plain");
response.getWriter().print("Hello app Engine");
}
}
Upvotes: 0