使用jetty测试执行http post的方法

时间:2014-06-09 10:52:09

标签: java junit jetty

我正在为执行HTTP post的程序编写junit测试,我正在尝试使用jetty来处理请求,但是我找不到关于如何将它用于此目的的好例子或解释,是否有人有如何做到这一点的好例子?

2 个答案:

答案 0 :(得分:0)

Junit是单元测试,而不是集成测试。集成测试意味着您需要启动并运行服务器来测试该功能。

在这里,您可以看到集成测试的示例:

     @Test
         public void test() {
                 HttpClient loggedClient = new HttpClient();
                 PostMethod loginPostMethod = new PostMethod(loginURL);
                 loginPostMethod.addParameter("j_username", login);
                 loginPostMethod.addParameter("j_password", password);
                 loginPostMethod.addParameter("remember", "1");
                 loginPostMethod.addParameter("clientType", clientType);
                 loginPostMethod.addParameter("clientVersion", clientVersion);
                  httpClient.executeMethod(postMethod);


                 String USERS_URL = HTTP_SERVER_DOMAIN + "/service";
                 PutMethod put = new PutMethod(USERS_URL);
                 put.addRequestHeader("Content-Type", "application/json");
                 put.setRequestBody("your_body");

                 try {
                     loggedClient.executeMethod(put);
                 } catch (HttpException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }

                 assertEquals(HttpStatus.SC_OK, put.getStatusCode());
}

答案 1 :(得分:0)

我不知道这是否可以帮助任何人,但这是我为我的问题提出的解决方案,所以我想我与你分享...... 我的问题是,在程序的一个特定部分,我试图用junit测试,它发出一个POST请求,我想验证它正在正确地发出请求..当然我可以用mockito来模拟正在发出请求的静态方法,但我只是认为使用像jetty这样的真实服务器会更好地测试我的程序发送的请求,这样,我的所有程序都会经过全面测试,而不会跳过它的任何部分......所以这里是我写的码码......

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class JettyHttpServer extends AbstractHandler
{
    private static Server server = null;

    public Server getServer()
    {
        return server;
    }

    public static void startServer(int webServerPort) throws Exception
    {
        System.out.println("Initializing server");
        server = new Server(webServerPort);
        server.setHandler(new JettyHttpServer());
        server.start();
    }

    public static void stopServer() throws Exception
    {
        try
        {
            server.stop();
        } catch (Exception ex)
        {
            ex.printStackTrace();
            throw new Exception("Could not stop server", ex);
        }
    }

    public void handle(String pathInContext, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException
    {
        System.out.println("inside Jetty Server...");
boolean requestWrongFlag = true

        if (httpRequest.getMethod().equals("POST"))
        {
            BufferedReader reader = httpRequest.getReader();
            String line;
            do {
            line = reader.readLine();
//do the validation here on the request received and in case it is not correct raise the requestWrongFlag
            System.out.println(line);
            }while (line!=null);


            httpResponse.setContentType("text/html;charset=utf-8");
           if(!requestWrongFlag)
               httpResponse.setStatus(HttpServletResponse.SC_ACCEPTED);
           else
                       httpResponse.setStatus(httpServletResponse.sc_BAD_REQUEST)
            httpResponse.getWriter().println("OK");
            request.setHandled(true);
        }
    }
}

然后我在调用被测程序之前就在我的TestCase中调用了jetty服务器...

如果有人有更好的建议......请与我分享......