我嘲笑一个HttpServletRequest,在servlet调用中有新的值在请求中设置,因为使用相同的请求我们正在请求某些jsp的请求,因此request对象被用作servlet的输入对象以及下一页的输出。如何使用Mockito部分模拟HttpServletRequest
我嘲笑所有输入参数,但对于所有了request.setAttribute(),我的代码是什么都不做,因为它是一个嘲弄类,说如果我有
request.setAttribute(a,"10")
System.out.println("a = " + request.getAttribute("a"));
我得到空因为我还没有给对于Request.getAttribute(“a”)来说任何行为都是显而易见的,我不能,它是我对下一页的响应,所以解释我需要2行为,因此我的请求对象部分模仿,并且我无法窥探或做任何部分嘲弄到目前为止。有任何想法吗?
代码:
//Testcase
Myservlet.java
public void doPost(request,response)
{
String a = request.getAttribute("a");
String b = request.getAttribute("b");
int sum = Integer.parseInt(a) + Integer.parseInt(b);
request.setAttribute("sum",sum);
//well in this example i can use sum what i calculated but in real senario i can't , i have to use request.getAttribute("sum")
insertSumIntoDB(request.getAttribute("sum"));
}
}
//testMyservlet.java
@test
public void testServlet()
{
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(request.getAttribute(a)).thenReturn("10");
when(request.getAttribute(b)).thenReturn("20");
new Myservlet(request,response);
}
2014-03-28
Vivek
+0
你能告诉你的嘲讽的尝试? –
+0
我改变了职位。请参阅insertSumIntoDB(request.getAttribute(“sum”))这实际上是insertSumintoDb(null),因为我没有给request.getAttribute(“sum”)的行为; –