• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing constructor with easymock

 
Andre Faber
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have the following problem testing my "BidImpl" class with easymock:
The constructor of my BidImpl class is "public BidImpl(UserImpl user, Auction auction, int amount)"
Where UserImpl is an implementation of the User interface.
Now I want to test this constructor by mocking the classes. But as it seems one cannot mock an Implementation of
a class but only interfaces.
Here is my code in JUnit:
public void testConstructeur(){
bid = new BidImpl(mockUser, mockAuction, 0); <- this is not working because mockUser is an interface
assertEquals(bid.getUser().getFirstName(), "André");
assertEquals(bid.getUser().getLastName(), "Faber");
verify(mockAuction);
}

Most likely this is simple to resolve and I just didn't understand how to use easymock rightly yet, so I apologize ahead for the question

Can anyone help me?

thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andre,
Why not change the constructor to:


The point of an interface is to make the code more generic. If the constructor doesn't use the interface, you lose this benefit.
 
Andre Faber
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, that would be a possibility, but I have to do this for school an don't know if I have the right to
change the sourcecode, but if there is no possibility to do this with the implementation I will do so...
 
Henri Tremblay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can mock a class. However you need to use EasyMock Class Extension for that (http://www.easymock.org/Downloads.html)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic