Reputation: 43
I have a JSP file that contains logic for generating HTML based on a list of objects that it receives from a servlet. In the servlet's doPost() method it sets up this list of objects and then sets those as a HttpServletRequest attribute as follows: request.setAttribute("objectList", objectList);
. Here is the relevent portion of JSP code that I have:
<c:set var="endSelect" value="${false }" />
<c:forEach items="${objectList }" var="object">
<c:choose>
<c:when test="${object.getType().equals("selectObject") }">
<c:if test="${!endSelect }">
<select id="${object.getId() }-detail" name="${object.getName() }" >
</c:if>
<option name="${object.getName() }" value="${object.getValue() }">
<c:out value="${object.getValue() }" />
</option>
<c:set var="endSelect" value="${true }" />
</c:when>
<c:otherwise>
<label for="${object.getId() }">
<c:out value="${child.getDecisionName() }" />
</label>
<input id="${object.getId() }" type="text" name="${object.getName() }" >
</c:otherwise>
</c:choose>
<c:if test="${endSelect }">
</select>
<c:set var="endSelect" value="${false }" />
</c:if>
</c:forEach>
As you can see, based on the object type, the JSP will either generate HTML for a select box or a text box.
Here's how I want to test this: I want to set up a unit test file where each test creates different objects (or mocks them, whichever), puts those objects in a list, then sends that list to my JSP file. I then want to validate the HTML that gets generated from my JSP.
The problem is that my JSP isn't being hosted on a server at all times. I can't just go get the URL. And I don't want to start the server and host the JSP page just for the test. I want to say, "Hey, here's a fake HttpServletRequest with some fake attributes set to it. Run that request through this JSP file and show me the generated HTML so that I can make sure there are select tags on certain tests and not others, and the end select tags show up where they are supposed to, the right number of text inputs are present, etc.."
I've looked in to Apache Cactus / Jakarta Cactus, JSoup, HttpUnit, Selenium, JUnit, and Mockito, but all of these have the problem of either needing the JSP to be hosted already, or they are "automated clicking" solutions, which isn't what I'm actually looking for.
Upvotes: 2
Views: 1464
Reputation: 9
You may want to check JspTester, a tool that allows to inject beans, session variables and request parameters directly into the JSP.
With JspTester, you will need to run the web application in a local web server like Tomcat (hope this form of "hosting" isn't against your requirements), then invoke JspTester's local URL. Once you run it, you will be provided with a top panel to enter the object needed for rendering the JSP, and click the "run" icon to render the JSP.
Upvotes: 0
Reputation: 160271
You don't really want to test JSP like this; testing a JSP is inherently an integration test unless you go to some lengths to extract a JSP rendering engine from something like Tomcat.
Other templating engines don't suffer from this limitation, e.g., FreeMarker, Handlebars, etc.
Also, you only need to start up a container once and run your entire suite of integration tests against it.
Upvotes: 2