1

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);

    }
}

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)

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

Is there a solution to get the executeAction() return the actual JSON response, rather than concatenating JSON responses from all previous executions.

4

1 回答 1

2

发生这种情况是因为您正在@Before方法中执行操作。这样,您的方法和测试setUp方法StrutsJUnit4TestCase之间就不会被调用,loginAdmin并且您之前的请求参数会再次传递给它。您可以setUp在测试方法中自己调用方法。在您的情况下,您实际上可以调用initServletMockObjects方法来创建新的模拟 servlet 对象,例如请求。

@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);

}
于 2013-11-13T21:16:48.990 回答