java - NPE when testing servlet with testNG and Mockito -


this question has answer here:

everyone! have problem tests servlets refreshed test, npe @ 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 @ com.epam.denys_leunov.java.lesson9.servlets.loginservlet.dopost(loginservlet.java:32) @ com.epam.denys_leunov.java.lesson9.servlets.testloginservlet.testdopostloginservlet(testloginservlet.java:61) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.testng.internal.methodinvocationhelper.invokemethod(methodinvocationhelper.java:85) @ org.testng.internal.invoker.invokemethod(invoker.java:639) @ org.testng.internal.invoker.invoketestmethod(invoker.java:816) @ org.testng.internal.invoker.invoketestmethods(invoker.java:1124) @ org.testng.internal.testmethodworker.invoketestmethods(testmethodworker.java:125) @ org.testng.internal.testmethodworker.run(testmethodworker.java:108) @ org.testng.testrunner.privaterun(testrunner.java:774) @ org.testng.testrunner.run(testrunner.java:624) @ org.testng.suiterunner.runtest(suiterunner.java:359) @ org.testng.suiterunner.runsequentially(suiterunner.java:354) @ org.testng.suiterunner.privaterun(suiterunner.java:312) @ org.testng.suiterunner.run(suiterunner.java:261) @ org.testng.suiterunnerworker.runsuite(suiterunnerworker.java:52) @ org.testng.suiterunnerworker.run(suiterunnerworker.java:86) @ org.testng.testng.runsuitessequentially(testng.java:1191) @ org.testng.testng.runsuiteslocally(testng.java:1116) @ org.testng.testng.run(testng.java:1024) @ org.testng.idearemotetestng.run(idearemotetestng.java:74) @ org.testng.remotetestngstarter.main(remotetestngstarter.java:121) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ com.intellij.rt.execution.application.appmain.main(appmain.java:144) enter code here enter code here

your problem in iuserdao dao;, initialized in method public void init(). so, rid of nullpointerexception have call init() before executing dopost

updated: try add in test before servlet.dopost(request, response);

servletcontext sc = servlet.getservletcontext(); servletcontext scspy = spy(sc); // return dao servlet context  doreturn(spy).when(scspy).getattribute("users"); servlet.init(); 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo