简体   繁体   中英

Writing Junit test cases for Struts

Can I write test cases for struts action class? That is, I would like to test execute method of an existing struts action class, where I can check what is the return ActionForward and also the content of form which is being set up in execute method. An example would be helpful.

Thanks Kamal

As far as I can tell, there is only StrutsTestCase . But this is for JUnit 3, not JUnit 4, so it seems to be pretty out of date.

The other alternatives you have come from How perform Junit tests with Struts - Ibatis , which suggest HttpUnit (again pretty old) or Cactus (which has been retired).

If I had to, I would start with StrutsTestCase . Here is a sample Test Case from the site:

public class TestLoginAction extends MockStrutsTestCase {
    public TestLoginAction(String testName) { super(testName); }

    public void testSuccessfulLogin() {
       setRequestPathInfo("/login");
       addRequestParameter("username","deryl");
       addRequestParameter("password","radar");
       actionPerform();
       verifyForward("success");
    }
}

I have used StrutsTestCase a long time ago, and as far as I can remember it worked fairly well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM