gpt4 book ai didi

java - 如何运行 jetty 服务器进行 java junit 测试

转载 作者:太空狗 更新时间:2023-10-29 22:50:50 27 4
gpt4 key购买 nike

我正在尝试编写一个集成测试以在本地启动 jetty 服务器,然后使用客户端与其余 URI 进行通信并调用下游的业务逻辑。然而,当我启动我的 jetty 服务器时,它并没有放弃控制权,所以我的客户端没有被执行。所以我使用线程在不同的线程中启动我的 jetty ,但是,线程在我的客户端调用之前完成,它说连接被拒绝。我可以采取什么方法?

@Test
public void testPerform() {

final JettyServer jettyServer = JettyServer.create();
jettyServer.buildJettyServer(ServletContextHandler.SESSIONS, "/", 8080, TestResource.class);

Runnable runnable = new Runnable()
{
@Override
public void run()
{
jettyServer.start();
}
};

new Thread(runnable).start();

final javax.ws.rs.client.Client client = ClientBuilder.newClient();

final Response response = client.target("http://localhost:8080/test").request().post(Entity.text(""));

jettyServer.stop();
}

最佳答案

跳过Runnable,跳过new Thread(runnable).start()

调用 jettyServer.start() 在它自己的线程上启动服务器(以及服务器需要的所有其他线程。

关于 junit 和 jetty 的基本示例 ...

@Test
public void testGet() throws Exception
{
// Create Server
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler();
ServletHolder defaultServ = new ServletHolder("default", DefaultServlet.class);
defaultServ.setInitParameter("resourceBase",System.getProperty("user.dir"));
defaultServ.setInitParameter("dirAllowed","true");
context.addServlet(defaultServ,"/");
server.setHandler(context);

// Start Server
server.start();

// Test GET
HttpURLConnection http = (HttpURLConnection)new URL("http://localhost:8080/").openConnection();
http.connect();
assertThat("Response Code", http.getResponseCode(), is(HttpStatus.OK_200));

// Stop Server
server.stop();
}

@Before@After junit 注释也可以使用。这将在每个 @Test 之前启动服务器并在之后停止服务器。

package jetty;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.net.HttpURLConnection;
import java.net.URL;

import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JUnitBeforeAfterJettyTest
{
private Server server;

@Before
public void startJetty() throws Exception
{
// Create Server
server = new Server(8080);
ServletContextHandler context = new ServletContextHandler();
ServletHolder defaultServ = new ServletHolder("default", DefaultServlet.class);
defaultServ.setInitParameter("resourceBase",System.getProperty("user.dir"));
defaultServ.setInitParameter("dirAllowed","true");
context.addServlet(defaultServ,"/");
server.setHandler(context);

// Start Server
server.start();
}

@After
public void stopJetty()
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}

@Test
public void testGet() throws Exception
{
// Test GET
HttpURLConnection http = (HttpURLConnection)new URL("http://localhost:8080/").openConnection();
http.connect();
assertThat("Response Code", http.getResponseCode(), is(HttpStatus.OK_200));
}
}

为了获得最佳方法,您还可以使用 @BeforeClass@AfterClass 技术,以及自动绑定(bind)到开放端口。这只会启动服务器一次,每个测试类,运行所有 @Test 方法,然后在最后停止一次服务器。

package jetty;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class JUnitBeforeAfterClassJettyTest
{
private static Server server;
private static URI serverUri;

@BeforeClass
public static void startJetty() throws Exception
{
// Create Server
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0); // auto-bind to available port
server.addConnector(connector);

ServletContextHandler context = new ServletContextHandler();
ServletHolder defaultServ = new ServletHolder("default", DefaultServlet.class);
defaultServ.setInitParameter("resourceBase",System.getProperty("user.dir"));
defaultServ.setInitParameter("dirAllowed","true");
context.addServlet(defaultServ,"/");
server.setHandler(context);

// Start Server
server.start();

// Determine Base URI for Server
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
serverUri = new URI(String.format("http://%s:%d/",host,port));
}

@AfterClass
public static void stopJetty()
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}

@Test
public void testGet() throws Exception
{
// Test GET
HttpURLConnection http = (HttpURLConnection) serverUri.resolve("/").toURL().openConnection();
http.connect();
assertThat("Response Code", http.getResponseCode(), is(HttpStatus.OK_200));
}
}

关于java - 如何运行 jetty 服务器进行 java junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29758607/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com