It helps to run your unit tests in the debugger. If you stepped onto that line that gets the NPE, you’ll see that “context” is null. The problem is, you’re creating a mocked context in your test, but you don’t store it anywhere that your CUT (code under test) can use it.
From: moc...@googlegroups.com [mailto:moc...@googlegroups.com]
On Behalf Of carlos hernando
Sent: Wednesday, January 06, 2016 2:40 PM
To: mockito <moc...@googlegroups.com>
Subject: [mockito] Re: when thenReturn always returns null
After resolve mi first mockito issue, i found my second one(very similar to my first one, but i don't know how to fix it)
I have this rest java function:
@GET
@Path("/deleteEmployee")
@Produces("application/json")
public ReturnCode deleteEmployee(@QueryParam("empId") String empIdToDelete)
{
ReturnCode returnCode = new ReturnCode(Constants.NO_ERROR_CODE, Constants.NO_ERROR_TEXT);
SessionFactory sessionFactory = (SessionFactory) context.getAttribute("SessionFactory");
and this test:
@Test
public void testDeleteServlet() throws Exception {
ServletContext context = mock (ServletContext.class, RETURNS_DEEP_STUBS);
SessionFactory factory = contextInitialized();
when(context.getAttribute("SessionFactory")).thenReturn(factory);
new EmployeeOps().deleteEmployee("33");
}
Why always crashes with null pointer in SessionFactory sessionFactory = (SessionFactory) context.getAttribute("SessionFactory");?
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/0dd07012-f7af-4786-8a6a-8b4542decb6c%40googlegroups.com.