lundi 7 mai 2018

Servlets unit testing

I have a problem with testing my servlets. I think it happened because I use context listner for init my dao, but I can't fix it. App works, but tests doesn't work.

One of my servlets, that show list of the users:

public class AllUsersServlet extends HttpServlet {
private static final String LIST_OF_USERS = "/WEB-INF/view//usersList.jsp";
private static final String DAO = "userDao";
private static final String USERS = "users";
private UserDaoImpl userDao;

@Override
public void init() throws ServletException {
    userDao = (UserDaoImpl) getServletContext().getAttribute(DAO);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    List<User> users = userDao.getAll();
    req.getServletContext().setAttribute(USERS, users);
    req.getRequestDispatcher(LIST_OF_USERS).forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doPost(req, resp);
}
}

One of my unit tests, that check AllUsersServlet:

public class AllUsersServletTest {
private static final String PAGE = "/WEB-INF/view//list.jsp";

@Test
public void whenDoGetThenReturnListPage() throws IOException, ServletException {
    AllUsersServlet allUsersServlet = new AllUsersServlet();

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    RequestDispatcher dispatcher = mock(RequestDispatcher.class);
    ServletContext context = mock(ServletContext.class);

    when(request.getServletContext()).thenReturn(context);
    when(request.getRequestDispatcher(PAGE)).thenReturn(dispatcher);

    allUsersServlet.doGet(request, response);

    verify(request, times(1)).getRequestDispatcher(PAGE);
    verify(request, never()).getSession();
    verify(dispatcher).forward(request, response);
}
}

So when I start my test it failed with nullpointer, stacktrace:

java.lang.NullPointerException
at ru.skilanov.controller.AllUsersServlet.doGet(AllUsersServlet.java:55)
at ru.skilanov.controller.AllUsersServletTest.whenDoGetThenReturnListPage(AllUsersServletTest.java:40)
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:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code -1

Aucun commentaire:

Enregistrer un commentaire