Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kedu par
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we write testclass( using JUnit4) to test Action class & DAO class in struts2.0 ? How?

Thanks.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. "How" depends on what, specifically, you're trying to test.
 
kedu par
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic