File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes Testing constructor with easymock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Testing constructor with easymock" Watch "Testing constructor with easymock" New topic

Testing constructor with easymock

Andre Faber

Joined: Dec 15, 2009
Posts: 2
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");

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?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Andre Faber

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

Joined: May 21, 2009
Posts: 7
Sure you can mock a class. However you need to use EasyMock Class Extension for that (

Henri Tremblay
Senior architect
Octo Technology
I agree. Here's the link:
subject: Testing constructor with easymock
It's not a secret anymore!