1

我编写了一个使用会话变量的 servlet。我正在为此流程创建 Junit 测试用例。但是如何在 Junit 测试用例中设置这些会话变量。我使用下面的代码在 Junit 测试用例中调用 servlet,

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPostReq = new HttpPost(/url);

    List<NameValuePair> reqParams = new ArrayList<NameValuePair>();
    nvpsReqParams.add(); // paramters to add

    httpPostReq.setEntity(new UrlEncodedFormEntity(reqParams, HTTP.UTF_8));
    HttpResponse response = httpClient.execute(httpPostReq);

在设置参数之后,我想添加实际用例所需的会话变量。有人可以帮我如何在此处添加会话变量。任何帮助将不胜感激。

4

1 回答 1

0

Your code seems as if you were trying to send a request to a servlet already deployed somewhere. It is not the purpose of unit tests. Rather than that, you should test your code (your servlet) in isolation, not deployed to a server.

For that, you need to use mock the web container interfaces your servlet uses. ServletUnit is a project that provides such mocks, perhaps have a look at that. Using them, you will be able to set your session parameters.

于 2013-02-22T11:22:00.683 回答