当前位置: 首页 > 知识库问答 >
问题:

测试登录Servlet

姜俊逸
2023-03-14

我试图使用mockito测试我的登录servlet,但它返回错误:

java.lang.NullPointerException:无法调用“java.io.PrintWriter.PrintLN(String)”,因为“out”为null。在Control.LoginPatient.DoPost(LoginPatient.java:55)在Control.LoginPatientTest.TestDoPostSuccessfull(LoginPatientTest.java:64)

下面是我的登录servlet的代码:

package control;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.ProfileManager;
import model.Bean.UserBean;

/**
 * Servlet implementation class LoginPatient
 */
@WebServlet("/LoginPatient")
public class LoginPatient extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginPatient() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String fiscal_code=request.getParameter("fiscal_code");
        String user_password= request.getParameter("user_password");
        PrintWriter out = response.getWriter();
        ProfileManager pM=new ProfileManager();
        UserBean patient= pM.ReturnPatientByKey(fiscal_code, user_password);

        if(patient != null) {
            HttpSession session= request.getSession();
            session.setAttribute( "user" , patient);
            session.setMaxInactiveInterval(-1);
            out.println("1");
        }

        else {
            out.println("0"); 
        }
    }

}

这里是我的测试用例:


class LoginPatientTest {

    private LoginPatient loginPatient;
    private HttpServletRequest mockedRequest;
    private HttpServletResponse mockedResponse;
    private ServletContext mockedServletContext;
    private HttpSession mockedSession;
    private PatientBean patient;
    private ProfileManager pm;
    private PrintWriter mockedOut;
    
    /**
     * @throws java.lang.Exception
     */
    @BeforeEach
    void setUp() throws Exception {
        loginPatient = new LoginPatient();
        mockedRequest = Mockito.mock(HttpServletRequest.class);
        mockedResponse = Mockito.mock(HttpServletResponse.class);
        mockedSession = Mockito.mock(HttpSession.class);
        mockedServletContext = Mockito.mock(ServletContext.class);
        mockedOut = Mockito.mock(PrintWriter.class);
        patient = Mockito.mock(PatientBean.class);
        pm = Mockito.mock(ProfileManager.class);
    }

    /**
     * @throws java.lang.Exception
     */
    @AfterEach
    void tearDown() throws Exception {
    }

    /**
     * Test method for {@link control.LoginPatient#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */ 
    @Test
    final void testDoPostSuccessful() throws ServletException, IOException{
        
Mockito.doReturn(mockedServletContext).when(mockedRequest).getServletContext();
        Mockito.when(mockedRequest.getParameter("fiscal_code")).thenReturn("fiscalCode");
        Mockito.when(mockedRequest.getParameter("user_password")).thenReturn("password");
        Mockito.when(mockedRequest.getSession()).thenReturn(mockedSession);
        Mockito.when(pm.ReturnPatientByKey("fiscalCode", "password"));
        Mockito.when(mockedResponse.getWriter()).thenReturn(mockedOut);

loginPatient.doPost(mockedRequest, mockedResponse);

        Mockito.verify(mockedOut).println("1");
        Mockito.verify(mockedSession).setAttribute(Mockito.eq("user"), patient);
    }

}

我不用spring

更新:现在它给了我另一个错误:

null

共有1个答案

班宏毅
2023-03-14

问题1:

在模拟设置中,将mockedOut变量更改为mockedOut=mockito.mock(printwriter.class);

问题2:

在激发业务逻辑之后,您将mockedout变量取出来。你应该换个顺序。在调用业务逻辑之前模拟所有内容。

//Given

    Mockito.doReturn(mockedServletContext).when(mockedRequest).getServletContext();
    Mockito.when(mockedRequest.getParameter("fiscal_code")).thenReturn("fiscalCode");
    Mockito.when(mockedRequest.getParameter("user_password")).thenReturn("password");
    Mockito.when(mockedRequest.getSession()).thenReturn(mockedSession);
    Mockito.when(pm.ReturnPatientByKey("fiscalCode", "password"));
    Mockito.when(mockedResponse.getWriter()).thenReturn(mockedOut);

//When   
    loginPatient.doPost(mockedRequest, mockedResponse);   


//Then
    Mockito.verify(mockedOut).println("1");
    Mockito.verify(mockedSession).setAttribute(Mockito.eq("user"), patient);
 类似资料:
  • 主要内容:录制登录测试,创建JMeter测试计划,添加监听器,验证输出本节试图解释使用任何公开可用的网站记录登录测试的确切步骤,该网站提供具有登录凭据的可靠登录页面。 出于测试目的,我们将使用OrangeHRM在URL- http://opensource.demo.orangehrmlive.com 下提供的公开网站来记录成功登录其网站。还将使用BlazeMeter提供的chrome扩展,通过该扩展可以在chrome中记录用户操作,然后导出测试脚本。 随后,可以在

  • 在本节中,将学习如何在Selenium IDE中创建登录测试用例。 出于测试目的,我们将测试考评师网 的登录页面,该网站网址如下: http://www.kaops.com/ 注意:可以在任何公开可用的网站上注册并创建登录测试。 下图显示了当点击上述URL时显示的主页的快照。 首先,注册用户以获取登录凭据。 对于此测试,我们已经生成了登录凭据(账户和密码)。 现在,生成一个测试脚本,以在Selen

  • 问题内容: 我正在开发Flask应用程序,并使用Flask-security进行用户身份验证(反过来又在下面使用Flask-login)。 我有一条需要身份验证的路由。我正在尝试编写一个单元测试,该测试对经过身份验证的用户返回适当的响应。 在单元测试中,我正在创建一个用户并以该用户身份登录,如下所示: 在测试内返回正确的用户。但是,请求的视图始终返回的。 所述路线定义为: 我可以肯定我只是不完全了

  • 我们有一个应用程序,有一个关键斗篷登录。我想创建一个JMeter测试,它使用一些凭据登录,而不是做一些事情。问题是我不知道如何形成帖子URL

  • 我正在尝试测试我的webapp中特定操作的性能。为了做到这一点,我必须首先登录。基本上我有两个HTTP-Request,一个用于登录,另一个我想使用多个同时线程(=用户)进行测试。 为了在第二个请求上获得可比的结果,我需要所有线程完成第一个请求,这样第一个请求就不会干扰第二个请求的性能。 我尝试将进程拆分为多个ThreadGroup或setUp-ThreadGroup和ThreadGroup,问题