Superceeded Article: Embedded Web Integration Testing with Jetty

Do you speak test? In that case: Hello web application:


public class WebIntegrationTest extends net.sourceforge.jwebunit.WebTestCase {

    public void testIndex() {
        beginAt("/index.html");
        assertTextPresent("Hello world");
    }

    private org.mortbay.jetty.Server server;

    protected void setUp() throws Exception {
        server = new org.mortbay.jetty.Server(0);
        server.addHandler(
                new org.mortbay.jetty.webapp.WebAppContext("src/main/webapp", "/my-context"));
        server.start();

        int actualPort = server.getConnectors()[0].getLocalPort();
        getTestContext().setBaseUrl("http://localhost:" + actualPort + "/my-context");
    }
}

This code runs with no application server, no separate deployment step, just like that.

If this looks interesting, see my full-sized article on java.net

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.

Print This Post Print This Post

5 Comments »

  1. Niraj Manandhar Said,

    January 20, 2007 @ 10:11 pm

    Thank you Mr. Johannes for this article

  2. Martin Gilday Said,

    February 28, 2007 @ 8:01 am

    Thanks for the article, been very useful in setting up our end to end tests.
    I am however having problems with getting JNID working within Jetty. Can you confirm the quoted code is correct?

  3. Johannes Brodwall Said,

    February 28, 2007 @ 8:18 am

    Hi, Martin

    I am pretty sure it is correct. But make sure that you use the latest version of this article: http://www.brodwall.com/johannes/blog/2007/02/04/updated-article-embedded-web-integration-testing-with-jetty/

    The code for this particular bit is here: http://svn.brodwall.com/demo/insanejava/trunk/web-demo/src/test/java/no/brodwall/web/integration/JettyTestServer.java

    If you still don’t have any success, feel free to post the problem here, or send me an email.

  4. suresh Said,

    March 14, 2007 @ 11:35 pm

    Thanks for the article. I ran into an issue with Jetty classloading when running on a linux platform.

    The code which retrieves Springs WebApplicatonContext using the jetty server’s servlet context was failing. Specifically, the following code was throwing a ClassCastException:

    ApplicationContext appctx = WebApplicationContextUtils
    .getRequiredWebApplicationContext(servletContext);

    The reason being that the Spring’s application context was loaded by Jetty’s own classloader, hence cannot be used here.

    The work around was to set the parentLoaderPriority to true while creating the Jetty server as described here: http://docs.codehaus.org/display/JETTY/Classloading

    eg:
    WebAppContext context = new WebAppContext();
    context.setParentLoaderPriority(true);
    context.setContextPath(”/”);
    context.setWar(”path/to/war/or/exploded/war”);
    jettyServer.setHandler(context);

    The errors happens didnt happen on a Windows XP box. But while running our CI builds on a Redhat EL4 box, the classcast exception was thrown.

    Redhat EL4
    JDK 1.5_07-b3
    Jetty-6.1.1
    Spring-Framework-2.0.2

    Hope this might help someone who runs into the same problem.

    – suresh –

  5. Johannes Brodwall Said,

    March 15, 2007 @ 4:50 pm

    Hi, Suresh.

    I was not aware of this issue. Thanks for the heads-up and the good feedback. I will have to work on how to integrate it into the text, but I have updated the source code in SVN.

    ~Johannes

RSS feed for comments on this post · TrackBack URI

Leave a Comment

Creative Commons Attribution 3.0 Unported
Creative Commons Attribution 3.0 Unported