vendredi 10 avril 2015

Struts2 Junit Plugin (StrutsTestCase) request object is Null

I am using the Struts2 JUnit Plugin (v2.3.20) struts2 (v.2.3.20) and have issues in my test class code.


I have List of jar files in my classpath:



-spring-beans-4.1.0.RELEASE.jar
-spring-context-4.1.0.RELEASE.jar
-spring-core-4.1.0.RELEASE.jar
-spring-mock-2.0.8.jar
-spring-test-4.1.0.RELEASE.jar
-spring-web-4.1.0.RELEASE.jar
-struts2-core-2.3.20.jar
-struts2-junit-plugin-2.3.20.jar
-struts2-spring-plugin-2.3.20.jar
-xwork-core-2.3.20.jar
-junit-4.12.jar


My struts.xml Action looks like the following:



<action name="createaccount" class="com.AccountAction">
<result name="success">/thankyou.jsp</result>
<result name="input">/createaccount.jsp</result>
</action>


And my Action class is as follows:



package com;
import java.util.logging.Logger;
import com.opensymphony.xwork2.ActionSupport;

public class AccountAction extends ActionSupport {

String userName;
String password;
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger( AccountAction.class.getName() );
//private Account accountBean;
public String execute(){
System.out.println("Execute Method");
return SUCCESS;
}

//Setter and getter methods for username and password
....
....

}


And my Test class is as follows:



package com;

import org.apache.struts2.StrutsTestCase;
import com.opensymphony.xwork2.ActionProxy;

public class TestAccountActionUsingStrutsTestCase extends StrutsTestCase {

public void testUserNameCorrect() throws Exception {

/**Here Request is coming null and it is throwing NullPointerException**/
//Exception here NullPointerException
//Exception here NullPointerException
request.setParameter("userName", "Bruce");
request.setParameter("password", "test");

ActionProxy proxy = getActionProxy("/createaccount");

AccountAction accountAction = (AccountAction) proxy.getAction();

String result = proxy.execute();

assertTrue("Problem There were errors present in fieldErrors but there should not have been any errors present", accountAction.getFieldErrors().size() == 0);
assertEquals("Result returned form executing the action was not success but it should have been.", "success", result);

}
public static void main(String[] args) throws Exception {
TestAccountActionUsingStrutsTestCase test=new TestAccountActionUsingStrutsTestCase();
test.testUserNameCorrect();
}
}


I use Spring within my struts app, and these jars are needed to use the mock request object and the StrutsTestCase.


As I commented in the above test class code , request is null in StrutsTestCase. Why is this?


Is that ritht place to put main method in Test class.? I am using right method of unit testing for sturts 2 and spring ?


and also why is request null? Any help will be Appreciated!


Aucun commentaire:

Enregistrer un commentaire