使用嵌入式Jetty测试Spring-Rest服务 [英] Test Spring-Rest Service with embedded Jetty

查看:98
本文介绍了使用嵌入式Jetty测试Spring-Rest服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个嵌入式码头服务器(不是maven,gradle等)来测试spring rest服务.

i want to create an embedded jetty server (not maven, gradle etc.) to test a spring rest service.

因此,我创建了EmbeddedServer类.但是不幸的是,调用其余服务总是导致http 404错误.怎么了?

Therefor i created an EmbeddedServer class. But unfortunately invocation of the rest service leads always to an http 404 error. What doing wrong?

rest服务可以与tomcat(未嵌入)一起正常工作.

The rest service works fine with tomcat (not embedded).

我使用以下依赖项:

"org.eclipse.jetty:jetty-server:8.1.17.v20150415"
"org.eclipse.jetty:jetty-webapp:8.1.17.v20150415"

这是EmbededServer类:

Here is the EmbededServer class:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class EmbeddedServer {

    private Server server;

    public void start() throws Exception {
        this.server = createServer();
        this.server.start();
    }

    public void stop() throws Exception {
        this.server.stop();
        this.server.join();
        this.server.destroy();
    }

    private Server createServer() {
        final Server server = new Server(8080);

        final WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        context.setResourceBase("src/main/webapp");
        context.setDescriptor("src/main/webapp/WEB-INF/web.xml");
        context.setParentLoaderPriority(true);
        context.setServer(server);

        server.setHandler(context);

        return server;
    }
}

和测试类:

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.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class SimpleTest {

    private HttpClient client;
    private EmbeddedServer server;

    @Before
    public void before() throws Exception {
        this.client = new DefaultHttpClient();
        this.server = new EmbeddedServer();
        this.server.start();
    }

    @After
    public void after() throws Exception {
        this.server.stop();
    }

    @Test
    public void invokeHello() throws Exception {
        final HttpResponse response = invokeGetRequest("http://localhost:8080/hello");
        verifyResponse(response, 200, "hello");
    }

    private HttpResponse invokeGetRequest(final String path) throws Exception {
        final URI wsAddress = new URI(path);
        final HttpGet method = new HttpGet(wsAddress);
        return this.client.execute(method);
    }

    private void verifyResponse(final HttpResponse actualHttpResponse, final int expectedHttpCode, final String expectedResponse) throws IOException {
        assertThat(actualHttpResponse.getStatusLine().getStatusCode(), equalTo(expectedHttpCode));

        final String actualResponse = EntityUtils.toString(actualHttpResponse.getEntity(), "UTF-8");
        assertThat(actualResponse, equalTo(expectedResponse));
    }
}

此测试失败,并显示:

java.lang.AssertionError: 
  Expected: <200>
  but: was <404>

推荐答案

ResourceBase和Descriptor的路径必须是绝对的.

Path of ResourceBase and Descriptor must be absolute.

context.setResourceBase("c:/myapp/src/main/webapp");
context.setDescriptor("c:/myapp/src/main/webapp/WEB-INF/web.xml");

这篇关于使用嵌入式Jetty测试Spring-Rest服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆