在解决了mi第一个mockito问题后,我发现了我的第二个问题(与第一个问题非常相似,但我不知道如何修复它)
我有这个rest java函数:
@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");
这个测试:
@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");
}
为什么总是在SessionFactory SessionFactory=(SessionFactory)context.getAttribute("SessionFactory");中使用空指针崩溃;?
其他mockito问题
已修复。
我已经更改了java应用程序,添加了方法
protected void setContext(ServletContext context)
{
this.context= context;
}
我用改变了测试
@Override
@BeforeClass
public void setUp() throws Exception {
context = mock (ServletContext.class, RETURNS_DEEP_STUBS);
employeeOps = new EmployeeOps();
employeeOps.setContext(context);
}
@Test
public void testDeleteServlet() throws Exception {
SessionFactory factory = contextInitialized();
when(context.getAttribute("SessionFactory")).thenReturn(factory);
employeeOps.deleteEmployee("41");
}
有了这个,效果很好。谢谢大家!