• Post Reply Bookmark Topic Watch Topic
  • New Topic

EasyMock Question .How to mock object when created with in method  RSS feed

 
Marius Chaminda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying introduce unit test and use easymock for the existing old code base . Please check one of my method in below. Here DAO class is created inside the method. There is no interface is define for DAO.

How can I mock DAO instance ?



public boolean saveData(String xml, String ds) {

boolean bReturn = false;
int iRequestSysid = 0;
try {
if (strXML != null) {
DAO dao = new DAO();
iRequestSysid = dao.save();
dao.parse();
bReturn = true;
}
}

catch (SQLException ex) {
System.out.println("An exception occured while calling the dao methods");
ex.printStackTrace();
}

return bReturn;
}
 
Joachim Rohde
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Chaminda wrote:
How can I mock DAO instance ?

You can't.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(The caveat being that you *can*, if you provide an alternative implementation that would be loaded before the real one, or just don't have the real implementation on the class during testing.)
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance of DAO should not be created inside the class under test, but provided via dependency injection, so you can inject the mock when testing and the implementation class otherwise.

Regards,
Uli
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!