StrutsTestCase implementation - Compilation error

826 Views Asked by At

I am using Struts 2 to create a web application. I am using StrutsTestCase for Junit test case to test the Action class. I have imported struts2-junit-plugin-2.3.4.jar as I am using struts2-core-2.3.4.jar. Inside the testcase method, when i tried to set the request parameters, request variable is not available for use. it is showing compilation error. I am getting 'request cannot be resolve' error. In my test class i am extending StrutsTestCase which has request as protected parameter. But it is not available inside extended method.

My test action looks like this:

import org.apache.struts2.StrutsTestCase;

public class WallPlanningActionTest extends StrutsTestCase {
    public void testList() {
    request.setParameter("salesOrg",1); 
}
2

There are 2 best solutions below

0
On

For future readers: I had the same problem - the protected request field was not accessible. The problem was that i hadn't add the spring libraries. StrutsTestCase uses spring-core-x.y.z and spring-test-x.y.z. It is depended on them an i couldn't find a way to use the unit tests without them. Other dependencies could be found by opening the struts-junit-plugin jar (as archive). Open the META-INF folder and in there you will find DEPENDENCIES file with a list of all dependencies. Hope this helps someone.

0
On

You can only get compilation errors if StrutsTestCase which your class is extended is not org.apache.struts2.StrutsTestCase. You could optimize the imports or just use FQCN.

public class WallPlanningActionTest extends org.apache.struts2.StrutsTestCase {