This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Testing constructor with easymock

 
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
 
author & internet detective
Posts: 39391
761
Eclipse IDE VI Editor Java
  • 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...
 
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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!