liveBook · Manning

JspTest

As explained in Test Driven: Practical TDD and Acceptance TDD for Java Developers

JspTest is an open-source Java library for compiling JavaServer Pages into servlet classes and invoking them in tests.

JspTest lets unit tests compile JavaServer Pages into servlet classes and invoke them without deploying to a full web container. Extend HtmlTestCase to render a JSP with a simulated request and check its HTML output. section 5.3.1 section 5.5

A small setup gets JSP tests ready:

  • Create a shared HtmlTestCase subclass and override getWebRoot so tests can use short JSP paths. section 5.3.1
  • Render the page, then assert that its login form contains the expected fields and submit button. section 5.3.1
import net.sf.jsptest.HtmlTestCase;

public abstract class MyJspTestCase extends HtmlTestCase {
    @Override
    protected String getWebRoot() {
        return "./websrc/jsp";
    }
}

This base class points JspTest at the JSP directory, so tests can request pages by shorter paths. section 5.3.1

Listing 5.16. Making use of our abstract JspTest base class
public class TestLoginPage extends MyJspTestCase { public void testFormFieldsArePresent() throws Exception { get("/login.jsp"); form().shouldHaveField("j_username"); form().shouldHaveField("j_password"); form().shouldHaveSubmitButton("login"); } }

A login-page test checks for the username, password, and submit controls. section 5.3.1

How Test Driven: Practical TDD and Acceptance TDD for Java Developers covers it

How deep it goes
Explains HtmlTestCase setup, web-root configuration, simulated requests, and HTML assertions, but not Jasper internals.
Examples
Uses a login form to test required fields and retaining a username after a failed login.
What you can do after
Apply JspTest through HtmlTestCase to render JSPs and assert form content and request-attribute-driven username retention.
How it connects
Connects JavaServer Pages with servlets, Jasper, simulated HTTP requests, HTML assertions, and test-driven development.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Test Driven: Practical TDD and Acceptance TDD for Java Developers. The section links point to the parts of the book it draws on.