| Author |
Testing constructor with easymock
|
Andre Faber
Greenhorn
Joined: Dec 15, 2009
Posts: 2
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26173
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Andre Faber
Greenhorn
Joined: Dec 15, 2009
Posts: 2
|
|
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
Joined: May 21, 2009
Posts: 7
|
|
|
Sure you can mock a class. However you need to use EasyMock Class Extension for that (http://www.easymock.org/Downloads.html)
|
Henri Tremblay
Senior architect
Octo Technology
|
 |
 |
|
|
subject: Testing constructor with easymock
|
|
|