2014-03-28 101 views
5

我嘲笑一个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); 
} 
+0

你能告诉你的嘲讽的尝试? – Mureinik

+0

我改变了职位。请参阅insertSumIntoDB(request.getAttribute(“sum”))这实际上是insertSumintoDb(null),因为我没有给request.getAttribute(“sum”)的行为; – Vivek

回答

5

你需要存储属性到一个集合:

// Collection to store attributes keys/values 
final Map<String, Object> attributes = new ConcurrentHashMap<String, Object>();  

// Mock setAttribute 
Mockito.doAnswer(new Answer<Void>() { 
    @Override 
    public Void answer(InvocationOnMock invocation) throws Throwable { 
     String key = invocation.getArgumentAt(0, String.class); 
     Object value = invocation.getArgumentAt(1, Object.class); 
     attributes.put(key, value); 
     System.out.println("put attribute key="+key+", value="+value); 
     return null; 
    } 
}).when(request).setAttribute(Mockito.anyString(), Mockito.anyObject()); 

// Mock getAttribute 
Mockito.doAnswer(new Answer<Object>() { 
    @Override 
    public Object answer(InvocationOnMock invocation) throws Throwable { 
     String key = invocation.getArgumentAt(0, String.class); 
     Object value = attributes.get(key); 
     System.out.println("get attribute value for key="+key+" : "+value); 
     return value; 
    } 
}).when(request).getAttribute(Mockito.anyString()); 
6

Spring的MockHttpServletRequestMockHttpServletResponse是为目的。例如。

MockHttpServletRequest request = new MockHttpServletRequest(); 
    MockHttpServletResponse response = new MockHttpServletResponse(); 
    request.addHeader(HttpHeaders.HOST, "myhost.com"); 
    request.setLocalPort(PORT_VALID); // e.g. 8081 
    request.setRemoteAddr(REQUEST_IP); // e.g. 127.0.0.1 

然后我可以调用myclass.method(请求,响应,...)并检查是否一些属性已被正确地设置到请求,例如

MyBean attr = (MyBean) request.getAttribute(ATTRIBUTE_NAME)); 
    // do my Assert.* stuff with 'attr' 

MockHttpServletRequest和MockHttpServletResponse做工精细,其中模拟(HttpServletRequest.class)失败,比如,你需要找回已经将业务逻辑中先前设置的请求属性的真正内容。

+4

MockHttpServletRequest和MockHttpServletResponse是Spring的一部分。如果你不使用Spring,它们不适用于你。 –

+0

@JimJarrett你是对的;另一方面,包含'MockHttpServlet ...'类的spring-test依赖关系相当轻量级(只需要spring-core),所以也许值得将它引入更好的测试的测试范围。 – jhyot