我对以下代码行有问题:
OrderForm of = (OrderForm)form;
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;
}
METHOD签名:
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
您不是将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);