2013-12-13 32 views
0

我有下面的行的代碼中的問題:投的Mockito ActionForm來

OrderForm of = (OrderForm)form; 

形式的ActionForm和OrderForm是擴展的ActionForm的類。

但我試圖測試這種方法,我不知道如何做到這一點。

對於參考,這是該方法:

OrderForm of = (OrderForm)form; 
     System.out.println(Integer.parseInt(getPublisherId(req))); 
     of.setPublisherId(Integer.parseInt(getPublisherId(req))); 

     AdvertiserManager am = new AdvertiserManager(); 
     OrderManager om = new OrderManager(); 
     of = om.getOrder(of, Locale.getDefault()); 
     //If no valid campaign was found the id of 'of' is 0. So if id = 0 return 404 status. 
     if (of.getId() == 0) { 
      response.setStatus(HttpServletResponse.SC_NOT_FOUND); 
      return null; 
     } 

方法簽名:

public ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) 

例外:

java.lang.ClassCastException: org.apache.struts.action.ActionForm$$EnhancerByMockitoWithCGLIB$$3538880f cannot be cast to xxx.forms.orders.OrderForm 
at xxx.IPOrderApiAction.performAction(IPOrderApiAction.java:82) 
at xxx.IPOrderApiActionTest.testGetOrderApiCallNotExistingCampaignId(IPOrderApiActionTest.java:39) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

測試:

private IPOrderApiAction spy; 

@Test 
public void testGetOrderApiCallNotExistingCampaignId() { 
    MockHttpServletRequest request = new MockHttpServletRequest(); 
    MockHttpServletResponse response = new MockHttpServletResponse(); 
    ActionMapping mapping = mock(ActionMapping.class); 
    ActionForm form = mock(ActionForm.class); 
    OrderForm of = mock(OrderForm.class); 
    Map<String,Object> map = new HashMap<String,Object>(); 
    map.put("id", "1261"); 
    spy = Mockito.spy(new IPOrderApiAction()); 
    Mockito.doReturn("1").when((IPAuthenticatedAction)spy).getPublisherId(request); 
    request.setParameters(map); 
    when(mapping.getParameter()).thenReturn("getOrder"); 
    spy.performAction(mapping,form, request, response); 
    assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); 
} 

它的struts 1.1

+0

有沒有例外?請顯示方法簽名。 –

回答

0

你不及格OrderForm的實例來你的方法,但ActionForm中的實例:

ActionForm form = mock(ActionForm.class); 
... 
spy.performAction(mapping,form, request, response); 

對於中投成功,形式必須是OrderForm的一個實例,所以你需要

ActionForm form = mock(OrderForm.class); 
... 
spy.performAction(mapping, form, request, response);