2014-03-28 100 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