Using Jetty Server with JUnit Checks

I am trying to test my web application by running the Jetty server in the BeforeClass method in my JUnit test case, and then using HttpClient to form a server request. I start the server without any problems, but I keep getting 404 when I try to make a request.

My server configuration is as follows:

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 working, and I see that I click it, it simply cannot find any resources.

+4
source share
4 answers

You probably need to define this servlet as a resource in the deployment descriptor (web.xml or jetty-web.xml). Just a hunch; 404 indicates that your code does not work at all, and therefore this is not a problem. The problem occurs before your code can even execute.

+3
source

This is the complete Junit test class that uses the 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(); } } 
+13
source

I started with a snippet of code in the first post, and then started to break through. The following code 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 is as follows:

 <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> 
+6
source

I think you forgot to install:

 wac.setWar("/path/to/war/file"); 

Or better, use the constructor:

 wac = new WebAppContext("/path/to/war/file", "/app"); 
0
source

Source: https://habr.com/ru/post/1343256/


All Articles