Kal*_*udi 19 java junit unit-testing
现在我正在编写我的类的测试用例.我想将HttpServletRequest对象参数传递给我的测试用例方法,以检查方法是否正常工作.所以任何人都给我这个建议.
public void testCheckBatchExecutionSchedule() throws Exception
{
assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request));
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*ner 43
Spring提供了一个名为MockHttpServletRequest的类,可用于测试需要HttpServletRequest的代码.
public void testCheckBatchExecutionSchedule() throws Exception
{
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("parameterName", "someValue");
assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request));
}
Run Code Online (Sandbox Code Playgroud)
Chi*_*hii 13
您应该使用模拟库来模拟请求对象,例如http://code.google.com/p/mockito/
public void testCheckBatchExecutionSchedule() throws Exception
{
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
//setup the behaviour here (or do it in setup method or something)
when(mockRequest.getParameter("parameterName")).thenReturn("someValue");
assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(mockRequest));
}
Run Code Online (Sandbox Code Playgroud)
HttpServletRequest是一个界面.在过去,我只是创建了一个类(例如TestHttpServletRequest),每个方法都有一个空方法体HttpServletRequest,除了我实际需要的那个.对于大多数方法,我返回了一个实例变量,并为该实例变量包含了一个setter,以便测试用例可以定义返回的内容. HttpServletRequest有很多方法,但大多数IDE(我使用Eclipse)都可以生成方法存根.
问题HttpServletRequestWrapper是它仍然需要将另一个HttpServletRequest传递给它的构造函数作为每个方法的默认行为.传递null结果NullPointerException.