简体   繁体   English

使用带有JUnit测试的Jetty服务器

[英]Using a Jetty Server with JUnit tests

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. 我一直试图通过在我的JUnit测试用例的BeforeClass方法中启动Jetty服务器然后使用HttpClient来形成对服务器的请求来测试我的Web应用程序。 I get the server to start without any issues, but I keep getting 404's when I try to make a request. 我让服务器启动没有任何问题,但是当我尝试发出请求时,我一直得到404。

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. 服务器正在运行,我可以看到我正在点击它,它只是找不到任何资源。

This is a complete Junit test class that uses jetty: 这是一个使用jetty的完整Junit测试类:

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();
    }

}

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: POM看起来像这样:

<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>

You probably need to define this servlet as a resource in your deployment descriptor (web.xml or jetty-web.xml). 您可能需要将此servlet定义为部署描述符(web.xml或jetty-web.xml)中的资源。 Just a guess; 只是一个猜测; a 404 indicates that your code isn't running at all, and therefore isn't the problem. 404表示您的代码根本没有运行,因此不是问题。 The problem is occurring before your code even has a chance to execute. 在您的代码甚至有机会执行之前,问题就出现了。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM