java 在 JUnit 测试中使用 Jetty 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5267423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:19:46  来源:igfitidea点击:

Using a Jetty Server with JUnit tests

javahttpjunitjetty

提问by stevebot

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.

我一直在尝试通过在 JUnit 测试用例的 BeforeClass 方法中启动 Jetty 服务器,然后使用 HttpClient 向服务器发送请求来测试我的 Web 应用程序。我让服务器毫无问题地启动,但是当我尝试发出请求时,我一直收到 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.

我的配置有问题吗?服务器正在运行,我可以看到我正在击中它,它只是找不到任何资源。

采纳答案by Travis Webb

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.

您可能需要将此 servlet 定义为部署描述符(web.xml 或 jetty-web.xml)中的资源。只是一个猜测;404 表示您的代码根本没有运行,因此不是问题。问题发生在您的代码甚至有机会执行之前。

回答by Christian Achilli

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

}

回答by OlliP

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>

回答by Binil Thomas

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