如何运行Jetty服务器进行Java Junit测试
我正在尝试编写一个集成测试以在本地启动Jetty服务器,然后使用客户端与其余URI进行通信并在下游调用业务逻辑。但是,当我启动码头服务器时,它不会放弃该控件,因此不会执行我的客户端。因此,我使用线程在另一个线程中启动我的码头,但是,该线程在我的客户端调用之前完成,它说连接被拒绝。我可以采取什么方法?
@Testpublic 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和码头的基本示例…
@Testpublic 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
技术,以及自动绑定到开放端口的方法。按照“测试类”,这只会启动服务器一次,运行所有@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));
}
}
以上是 如何运行Jetty服务器进行Java Junit测试 的全部内容, 来源链接: utcz.com/qa/404637.html