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
This work is licensed under a
Creative Commons Attribution 3.0 License.
Print This Post
Add New Comment
Viewing 5 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
I am however having problems with getting JNID working within Jetty. Can you confirm the quoted code is correct?
Do you already have an account? Log in and claim this comment.
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/0...
The code for this particular bit is here: http://svn.brodwall.com/demo/insanejava/trunk/w...
If you still don't have any success, feel free to post the problem here, or send me an email.
Do you already have an account? Log in and claim this comment.
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/Classloa...
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 --
Do you already have an account? Log in and claim this comment.
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
Add New Comment