Java 使用JMock模拟HttpServletRequest
我正在尝试对一个方法进行单元测试,我需要使用构造函数为该方法创建类的对象。构造函数是这样的:Java 使用JMock模拟HttpServletRequest,java,unit-testing,jmock,Java,Unit Testing,Jmock,我正在尝试对一个方法进行单元测试,我需要使用构造函数为该方法创建类的对象。构造函数是这样的: public WebformsClinEventBusiness(Webform webForm, String dataEntryModel, String obsBatId) { this.webformObj = webForm; this.eventObj = webForm.getEvent(obsBatId); this.prog
public WebformsClinEventBusiness(Webform webForm, String dataEntryModel, String obsBatId) {
this.webformObj = webForm;
this.eventObj = webForm.getEvent(obsBatId);
this.progressNoteText = webForm.getProgressNoteNext();
this.formValues = webForm.getFormValues();
this.lastFormValues = webForm.getLastFormValues();
this.emptyFormValues = webForm.getEmptyFormValues();
this.formVersion = webForm.getFormVersion();
this.dataEntryModel = dataEntryModel;
this.headerValues = webForm.getHeaderValues();
this.OBS_BAT_ID = obsBatId;
this.userPreferences = (CompSoapUserPreferences)webForm.getRequest().getSession(false).getAttribute(userPreferences.getSESName());
}
问题在最后一行。我有一个模拟webform对象,并设置了如下期望:
one (mockWebForm).getRequest();
will(returnValue(mockRequest));
one (mockRequest).getSession(false);
will(returnValue(mockSession));
allowing (mockSession).getAttribute(_SES_NAME);
will(returnValue(mockCompSoapUserPreferences));
allowing (mockCompSoapUserPreferences).getSESName();
will(returnValue(_SES_NAME));
当代码点击webform.getRequest()而不是调用mock时,它会调用实际的方法,该方法返回null,最后出现null指针异常
有什么想法吗?我真的非常感谢您对此的任何意见
谢谢你的时间 您是如何声明webForm的?webForm声明为webForm mockWebForm=context.mock(“webForm.class”);到目前为止还没有发现任何问题,您能发布更多类似deatil的测试用例和stacktraces吗?WebForm是一个类还是一个接口?