如何运行码头服务器进行java junit测试

2022-09-02 12:18:26

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

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

答案 1

跳过 ,跳过Runnablenew Thread(runnable).start()

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

对于朱尼特和码头的基本示例...

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

也可以使用 和 junit 注释。这将在每个服务器之前启动服务器,并在之后停止服务器。@Before@After@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));
    }
}

答案 2