Struts2 Junit4 测试在每次操作执行时累积 JSON 响应 [英] Struts2 Junit4 tests accumulate JSON responses with every action execution
问题描述
我写了一些 Junit4 测试,看起来像这样:
I've written a few Junit4 tests, which looks like this :
public class TestCampaignList extends StrutsJUnit4TestCase<Object> {
public static final Logger LOG = Logger.getLogger(TestCampaignList.class.getName());
@Before
public void loginAdmin() throws ServletException, UnsupportedEncodingException {
request.setParameter("email", "nitin.cool4urchat@gmail.com");
request.setParameter("password", "22");
String response = executeAction("/login/admin");
System.out.println("Login Response : " + response);
}
@Test
public void testList() throws Exception {
request.setParameter("iDisplayStart", "0");
request.setParameter("iDisplayLength", "10");
String response = executeAction("/campaign/list");
System.out.println("Reponse : " + response);
}
}
两个操作都返回 JSON 结果,executeAction
javadoc 说:
Both actions return JSON results and executeAction
javadoc says :
For this to work the configured result for the action needs to be FreeMarker, or Velocity (JSPs can be used with the Embedded JSP plugin)
似乎无法处理JSON结果,因此,第二个动作执行显示累积结果,例如result_for_second_action= result1 concatenate result2
Seems like it's unable to handle JSON results and hence, the second action execution shows accumulated result, such that result_for_second_action= result1 concatenate result2
有没有办法让 executeAction()
返回实际的 JSON 响应,而不是连接所有先前执行的 JSON 响应.
Is there a solution to get the executeAction()
return the actual JSON response, rather than concatenating JSON responses from all previous executions.
推荐答案
发生这种情况是因为您正在 @Before
方法中执行操作.这样,StrutsJUnit4TestCase
的 setUp
方法不会在您的 loginAdmin
和测试方法之间被调用,并且您之前的请求参数会再次传递给它.您可以在测试方法中自行调用 setUp
方法.在您的情况下,您实际上可以调用 initServletMockObjects
方法来创建新的模拟 servlet 对象,例如请求.
This is happening because you are executing action in @Before
method. In that way the setUp
method of StrutsJUnit4TestCase
is not getting called in between your loginAdmin
and test method and you previous request parameters are passed to it again. You can call setUp
method by yourself in your tests method.
In your case you can actually call initServletMockObjects
method to create new mock servlet objects such as request.
@Test
public void testList() throws Exception {
setUp();
// or
// initServletMockObjects();
request.setParameter("iDisplayStart", "0");
request.setParameter("iDisplayLength", "10");
String response = executeAction("/campaign/list");
System.out.println("Reponse : " + response);
}
这篇关于Struts2 Junit4 测试在每次操作执行时累积 JSON 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!