使用嵌入式Jetty测试Spring-Rest服务

成吉思

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

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

其余的服务可以与tomcat(未嵌入)一起正常工作。

我使用以下依赖项:

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

这是EmbededServer类:

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的路径必须是绝对路径。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

嵌入式Jetty服务器

Jetty嵌入式服务器和Spring Security集成

如何使用嵌入式Jetty设置静态资源和自定义服务?

在Docker上使用嵌入式Jetty服务器运行Java应用程序

在 Ktor 中使用 Jetty 嵌入式服务器获取客户端证书

无法启动嵌入式Jetty服务器

启动嵌入式Jetty服务器的最短代码

嵌入式 Jetty 9 - 我需要 Websocket 服务器、REST Servlet 和文件服务器

使用`System.exit()`停止具有嵌入式Jetty服务器的Java应用程序是否安全?

使用Spring Boot在嵌入式Tomcat服务器上部署资源

如何关闭Spring Boot嵌入式服务器

嵌入式实体的Spring Data Rest投影

在内存/嵌入式SFTP服务器中,用于使用Java中的私钥身份验证进行测试

如何创建嵌入式WebSocket服务器Jetty 9?

如何使用嵌入式码头服务index.html?

使用FastCGI的嵌入式Web服务器

测试REST Web服务

Spring REST服务的LazyInitializationException

嵌入式Java HTTPS服务器进行集成测试

无法为 Kafka 启动嵌入式服务器以进行 Junit 测试

运行单元测试时是否可以阻止嵌入式服务器启动?

弹簧启动测试和嵌入式弹性服务器

Spring Boot服务器无法启动嵌入式Tomcat服务器

尝试通过嵌入式Jetty服务器运行Ktor应用时如何避免java.lang.ExceptionInInitializerError?

嵌入式Jetty服务器-没有对/的JSP支持,找不到org.apache.jasper.servlet.JspServlet

使业务域对象可用于嵌入式Jetty服务器中的Jersey Servlet上下文

Spring Boot是否以战争方式部署在外部服务器上时使用外部服务器还是嵌入式服务器?

嵌入 angular 或使用嵌入式 URL REST API 后,如何从嵌入式 Power BI 报告/视觉对象中读取数据点值详细信息?

Nginx Plus无法使用Docker嵌入式DNS服务器解析服务