title: Unit Testing JSP file
body: <p>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: <code>request.setAttribute("objectList", objectList);</code>.  Here is the relevent portion of JSP code that I have:</p>
  
  <pre><code>&lt;c:set var="endSelect" value="${false }" /&gt;
  &lt;c:forEach items="${objectList }" var="object"&gt;
    &lt;c:choose&gt;
      &lt;c:when test="${object.getType().equals("selectObject") }"&gt;
        &lt;c:if test="${!endSelect }"&gt;
          &lt;select id="${object.getId() }-detail" name="${object.getName() }" &gt;
        &lt;/c:if&gt;
        &lt;option name="${object.getName() }" value="${object.getValue() }"&gt;
          &lt;c:out value="${object.getValue() }" /&gt;
        &lt;/option&gt;
        &lt;c:set var="endSelect" value="${true }" /&gt;
      &lt;/c:when&gt;
      &lt;c:otherwise&gt;
        &lt;label for="${object.getId() }"&gt;
          &lt;c:out value="${child.getDecisionName() }" /&gt;
        &lt;/label&gt;
        &lt;input id="${object.getId() }" type="text" name="${object.getName() }" &gt;
      &lt;/c:otherwise&gt;
    &lt;/c:choose&gt;
    &lt;c:if test="${endSelect }"&gt;
      &lt;/select&gt;
      &lt;c:set var="endSelect" value="${false }" /&gt;
    &lt;/c:if&gt;
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>As you can see, based on the object type, the JSP will either generate HTML for a select box or a text box.</p>
  
  <p>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.</p>
  
  <p>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.."</p>
  
  <p>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.</p>