user1752078
user1752078

Reputation: 95

How to write junit test for struts 2 action class?

I want to write the junit test for struts 2 action class methods.How to write test case for addUser function? My action class look like this

puble class UserAction{

public String addUser(){
User user = new User();
user.setUserName("user");
user.setPassword("password");
UserDAO userDAO = new UserDAO()
userDAO.addUser(user)
return SUCCESS;
}

Upvotes: 4

Views: 5748

Answers (2)

Arvind
Arvind

Reputation: 1568

Struts 2 provides a JUnit plugin library that makes it easier to develop unit tests for testing your Struts 2 action classes. to test your action you need to know which aspect action class required in order to run . you can check this example

http://struts.apache.org/release/2.2.x/docs/struts-2-junit-plugin-tutorial.html

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160191

Here you're forced to write an integration test since you've hard-coded a DAO implementation. (There are ways around this, but writing better actions is a better approach.)

Instead of using an explicit DAO implementation, inject one. To unit test this action you'd want to simulate both the success and failure of adding the user.

Then check the action's return value.

Upvotes: 1

Related Questions