Reputation: 24035
I've been trying to test my web app by starting a Jetty server in the BeforeClass method of my JUnit test case and then using a HttpClient to form request to the server. I get the server to start without any issues, but I keep getting 404's when I try to make a request.
The configuration of my server is like the following:
public void start() throws Exception {
if (server == null) {
server = new Server(PORT);
server.setStopAtShutdown(true);
wac = new WebAppContext();
wac.setContextPath("/app");
wac.setResourceBase("war");
wac.setClassLoader(this.getClass().getClassLoader());
server.addHandler(wac);
server.start();
}
}
Is there something wrong with my config? The server is running, and I can see that I am hitting it, it just can't find any resources.
Upvotes: 4
Views: 16389
Reputation: 1595
I started with the code snippet in the first post and then started to fight my way through. The code below finally worked for me:
Server server = new Server(8080);
server.setStopAtShutdown(true);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/app");
webAppContext.setResourceBase("src/main/webapp");
webAppContext.setClassLoader(getClass().getClassLoader());
server.addHandler(webAppContext);
server.start();
URL url = new URL("http://localhost:8080/app/some_call");
URLConnection connection = url.openConnection();
List<String> lines = IOUtils.readLines(connection.getInputStream());
System.out.println(lines.get(0));
POM looks like this:
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
</dependency>
<jetty.version>6.1.25</jetty.version>
Upvotes: 6
Reputation: 5727
This is a complete Junit test class that uses jetty:
package test.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;
public class MockPortalTest {
private Server server;
@Before
public void startServer() throws Exception {
server = new Server(8080);
server.setStopAtShutdown(true);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/app");
webAppContext.setResourceBase("src/main/webapp");
webAppContext.setClassLoader(getClass().getClassLoader());
server.addHandler(webAppContext);
server.start();
}
@Test
public void shouldBePreAuthenticated() throws Exception {
String userId = "invalid";
HttpClient client = new DefaultHttpClient();
HttpGet mockRequest = new HttpGet("http://localhost:8080/app");
mockRequest.setHeader("http-user",userId);
HttpResponse mockResponse = client.execute(mockRequest);
BufferedReader rd = new BufferedReader
(new InputStreamReader(mockResponse.getEntity().getContent()));
// DO YOUR ASSERTIONS
}
@After
public void shutdownServer() throws Exception {
server.stop();
}
}
Upvotes: 14
Reputation: 13809
I think you forgot to set:
wac.setWar("/path/to/war/file");
Or better, use the constructor:
wac = new WebAppContext("/path/to/war/file", "/app");
Upvotes: 0
Reputation: 15028
You probably need to define this servlet as a resource in your deployment descriptor (web.xml or jetty-web.xml). Just a guess; a 404 indicates that your code isn't running at all, and therefore isn't the problem. The problem is occurring before your code even has a chance to execute.
Upvotes: 3