I'm trying to test a jsp page.
What I understand this does is that a request will be sent to /test which will be caught by MyServlet, this part works. However the probleem seems to be that MyServlet doesn't seem to be able to find foo.jsp as the assertion fails because the actual response is null.
So I hope this is just a simple matter of referencing the jsp file in the wrong way. My foo.jsp (which just contains hello in the body), is under the webapp/WEB-INF. I've also tried putting it under the test resources folder as well as under the main resources folder.
This is what I've come up with so far:
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.testing.ServletTester;
import foo.MyServlet;
public class EncodeForHTMLTagTest extends TestCase{
private ServletTester tester;
private HttpTester request;
private HttpTester response;
public EncodeForHTMLTagTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
tester = new ServletTester();
tester.setContextPath("/");
tester.addServlet(MyServlet.class, "/test");
tester.start();
request = new HttpTester();
request.setMethod("GET");
request.setHeader("Host", "tester");
request.setVersion("HTPP/1.0");
response = new HttpTester();
}
@Override
protected void tearDown() throws Exception {
}
public static Test suite() {
TestSuite suite = new TestSuite(EncodeForHTMLTagTest.class);
return suite;
}
public void testHtmlEncode() throws Exception {
request.setURI("/test");
response.parse(tester.getResponses(request.generate()));
assertEquals(200, response.getStatus());
assertEquals("Hello", response.getContent());
}
}
package foo.MyServlet;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Hello world!");
resp.setContentType("text/html;charset=UTF-8");
getServletContext().getRequestDispatcher("/foo.jsp").forward(req, resp);
}
}
When I deploy the application to tomcat using following web.xml file, the jsp page is retrieved correctly when adding /test to the url.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://ift.tt/nSRXKP"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/LU8AHS">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.dhl.itss.security.tags.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Aucun commentaire:
Enregistrer un commentaire