Struts2 Junit4测试会在每次操作执行时累积JSON响应

Struts2 Junit4测试会在每次操作执行时累积JSON响应,struts2,struts2-json-plugin,struts2-junit-plugin,Struts2,Struts2 Json Plugin,Struts2 Junit Plugin,我已经编写了一些Junit4测试,如下所示: public class TestCampaignList extends StrutsJUnit4TestCase<Object> { public static final Logger LOG = Logger.getLogger(TestCampaignList.class.getName()); @Before public void loginAdmin() throws ServletExcept

我已经编写了一些Junit4测试,如下所示:

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结果,因此,第二个操作执行显示累积结果,例如
result\u for\u second\u action=result1 concatenate result2


是否有一种解决方案可以让
executeAction()
返回实际的JSON响应,而不是将所有以前执行的JSON响应串联起来。

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

}

这就解决了问题。虽然我在之前把
@取错了,但实际上它是在每次测试之前执行的。在模拟对象
请求
不可用之前,我无法使用
@BeforeClass
。有什么东西在所有测试开始前我只能使用一次吗?@coding\u idiot:根本不要调用登录操作。而是执行不带操作的登录逻辑。
@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);

}