Java 如何为Struts 2编写行为测试?
我正在用cucumber jvm为一个使用Struts 2的遗留项目编写行为测试 我已经成功地使用jvm测试了应用程序某个特性的行为。但我认为我选择了错误的起点:我目前正在创建操作并直接调用其方法,例如:Java 如何为Struts 2编写行为测试?,java,struts2,cucumber-jvm,Java,Struts2,Cucumber Jvm,我正在用cucumber jvm为一个使用Struts 2的遗留项目编写行为测试 我已经成功地使用jvm测试了应用程序某个特性的行为。但我认为我选择了错误的起点:我目前正在创建操作并直接调用其方法,例如: // Mock a session session = Mockito.mock(HttpSession.class); // mock a request request = Mockito.mock(HttpServletRequest.class); Mockito.when(requ
// Mock a session
session = Mockito.mock(HttpSession.class);
// mock a request
request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getSession()).thenReturn(session);
// create a context
Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put(StrutsStatics.HTTP_REQUEST, request);
ActionContext.setContext(new ActionContext(contextMap));
ActionContext.getContext().getActionInvocation();
用户。功能
Given that I am in the Users Page (I create a UserAction object in my Step Definition)
When I press "list" (I call the userAction.list() method)
Then I should see the following users: // list of users
问题是,随着我测试越来越多具有“列表”方法的功能,我的步骤定义将变得越来越复杂,因为我必须确定需要调用哪个操作的方法
解决方案是像jsp页面那样动态调用这些操作。如何从Java中实现这一点?我是Struts 2的新手,到目前为止,我发现了一些关于jsp页面的DispatchAction教程
我已经有了一个ActionContext、请求和会话,例如:
// Mock a session
session = Mockito.mock(HttpSession.class);
// mock a request
request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getSession()).thenReturn(session);
// create a context
Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put(StrutsStatics.HTTP_REQUEST, request);
ActionContext.setContext(new ActionContext(contextMap));
ActionContext.getContext().getActionInvocation();
//模拟会话
session=Mockito.mock(HttpSession.class);
//冒充请求
request=Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getSession()).thenReturn(session);
//创建上下文
Map contextMap=newhashmap();
put(strutstatics.HTTP_请求,请求);
setContext(新的ActionContext(contextMap));
ActionContext.getContext().getActionInvocation();
Struts 2中没有DispatchAction。不过,我对这个问题不太了解。@DaveNewton Struts2上的所有操作都提供了这个功能。我面临的问题是如何动态调用我的操作,就像从jsp页面调用一样。你现在上课了吗?