aspose file tools*
The moose likes Testing and the fly likes Junit4- struts2.0 -test Action class & DAO class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit4- struts2.0 -test Action class & DAO class" Watch "Junit4- struts2.0 -test Action class & DAO class" New topic
Author

Junit4- struts2.0 -test Action class & DAO class

kedu par
Greenhorn

Joined: Nov 21, 2007
Posts: 11
can we write testclass( using JUnit4) to test Action class & DAO class in struts2.0 ? How?

Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes. "How" depends on what, specifically, you're trying to test.
kedu par
Greenhorn

Joined: Nov 21, 2007
Posts: 11
1) My action class contain something like this

public String login()
{
Create object of DAO class(DAOref)
int temp=DAOref.validate(username,password);
if(temp==1)
{
return "ABC";
}
if(temp==2)
{
return "XYZ";
}
}



2) My DAO class coantain something like this

public int validate( String username, String password)
{
validate username & password with database & return corrosponding id;
return id;

}


How we can test above action class & DAO class?


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Testing the action is trivial; use a mock DAO to provide the response you want.

How to test the DAO depends on what you actually want to test.
kedu par
Greenhorn

Joined: Nov 21, 2007
Posts: 11
Thanks David Newton for your quick reply.

But still i am having one question..


I want to test username & password from DAO class(validate method)
I am having those values in login table database.
should I check those values in Test class by taking expected data as "hard coded" values? OR how?


Thanks....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Again--it depends on what you're trying to test. If you're trying to test the DAO's interaction with the database, that's a completely different issue (and not really a unit test, at least by my definition--that'd be an integration test). If you want to test your action, you don't *need* to go to the database--and for a unit test, I try to avoid database access because of the additional time overhead it would add.
 
 
subject: Junit4- struts2.0 -test Action class & DAO class