Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EasyMock, mixing class extenstion and normal mocking in same unit test

 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am having a problem with EasyMock, I have a test that requires the mocking of an object (I have achived this).
However the Object (A) then calls another object (I) (which has an interface) which should throw an exception.

Now both A and I both implment the same interface, what has happened is i am wrapping/decorating(?) an object of type I to provide some logging.

So the client calls A.methodX, which maps though to I.methodX, so I would expect two calls to methodX.

however EasyMock only seems to recognise the calls to A and not to the internal object of A that is represented by I.

Sorry I dont know how to make that make anymore sense.

Any ideas?
I am on the verge of just making teh whole test use class Extension.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small code example to help:



A is being moced as classes extension, while something/I is using normal EasyMock.

Thanks
[ May 27, 2008: Message edited by: Gavin Tranter ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gavin,
Can you post a bit of test code that illustrates the problem?
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,
Seems it was user error :S
I kinda got locked in on my mock objects, but the object I was mocking using class extension I actually want to fully test rather then mock!!

Here is what I was attempting, I still get Expectation errors, but I think it is down to loosing sight of teh woods for the trees.
I tried to keep it genric the example generic and compact.

Thanks
Gavin

 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you ask EasyMock to create a mock object for "ClassToMock.class", you're effectively getting back a completely mocked out fake thing. In other words, whatever methods you invoke on that mock object, the only thing that happens is what would happen with a mock created for an interface -- the original code is not executed.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lasse,
Yeah I got that now, I kinda of knew that, but this is my first time with easyMock.

What happened was cos I was mocking everything else, I just mocked what I changed and I didnt realise that my purpose was actual to test what I had change not mock out I had changed

I felt a teeny bit dumb when one of the senior guys pointed this out
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic