0

everyone! I have a problem with tests servlets I REFRESHED MY TEST, NPE at this line in Servlet "session.setAttribute("login", user.getLogin());"

My servlet:

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

private IUserDao dao;

@Override
public void init() throws ServletException {
    dao = (IUserDao) getServletContext().getAttribute("Users");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession session = request.getSession();

    if (dao.isUserExists(request.getParameter(LOGIN), request.getParameter(PASSWORD))) {
        User user = dao.getUser(request.getParameter(LOGIN));

        session.setAttribute("login", user.getLogin());
        session.setAttribute("message", "you logged in successfully!");
        response.sendRedirect("hello.jsp");
        return;
    }

    User notUser = new User(request.getParameter(LOGIN), request.getParameter(PASSWORD));
    session.setAttribute("user", notUser);
    session.setAttribute("wrongUserCredentials", "Wrong login or password");
    response.sendRedirect("login.jsp");

}

}

`

My test:

@Test(groups = "positive")
public class TestLoginServlet extends Mockito {

private static final Logger LOGGER = Logger.getRootLogger();
@Mock
private IUserDao dao;




@BeforeClass
public void beforeClass() {
    MockitoAnnotations.initMocks(this);

}

@Test
public void testDoPostLoginServlet() throws Exception {

            HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    HttpSession session = mock(HttpSession.class);
    ServletConfig config = mock(ServletConfig.class);
    ServletContext context = mock(ServletContext.class);

    UserDaoInMemory daoInMemory = new UserDaoInMemory();
    UserDaoInMemory spy = spy(daoInMemory);
    User user = new User("kote", "Qw12");

    when(config.getServletContext()).thenReturn(context);
    when(request.getParameter(LOGIN)).thenReturn(user.getLogin());
    when(request.getParameter(PASSWORD)).thenReturn(user.getPassword());
    when(request.getSession()).thenReturn(session);
    when(request.getServletContext()).thenReturn(context);
    when(context.getAttribute("Users")).thenReturn(spy);
    doReturn(true).when(spy).isUserExists(user.getLogin(), user.getPassword());

    LoginServlet servlet = new LoginServlet();
    servlet.init(config);
    servlet.doPost(request, response);

    verify(request, atLeast(1)).getParameter(LOGIN);
    verify(request, atLeast(1)).getParameter(PASSWORD);
    LOGGER.info("Passed");
}

}

Output: java.lang.NullPointerException at com.epam.denys_leunov.java.lesson9.servlets.LoginServlet.doPost(LoginServlet.java:32) at com.epam.denys_leunov.java.lesson9.servlets.TestLoginServlet.testDoPostLoginServlet(TestLoginServlet.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) at org.testng.TestRunner.privateRun(TestRunner.java:774) at org.testng.TestRunner.run(TestRunner.java:624) at org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at org.testng.SuiteRunner.run(SuiteRunner.java:261) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1191) at org.testng.TestNG.runSuitesLocally(TestNG.java:1116) at org.testng.TestNG.run(TestNG.java:1024) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) enter code here enter code here

  • I have a problem at this line if (dao.isUserExists(request.getParameter(LOGIN), request.getParameter(PASSWORD))), why it isn't mock by this doReturn(true).when(spy).isUserExists("kote","Qw12"); – Dan Batman Jun 01 '16 at 20:39

1 Answers1

0

Your problem is in IUserDao dao;, which is initialized in method public void init(). So, to get rid of NullPointerException you have to call init() before executing doPost

Updated: Try to add in test before servlet.doPost(request, response);

ServletContext sc = servlet.getServletContext();
ServletContext scSpy = spy(sc);
// return dao from servlet context 
doReturn(spy).when(scSpy).getAttribute("Users");
servlet.init();
Solorad
  • 904
  • 9
  • 21