It's not a secret anymore!*
The moose likes Testing and the fly likes EasyMock, mixing class extenstion and normal mocking in same unit test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "EasyMock, mixing class extenstion and normal mocking in same unit test" Watch "EasyMock, mixing class extenstion and normal mocking in same unit test" New topic
Author

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

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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

Joined: Jan 01, 2007
Posts: 333
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

Gavin,
Can you post a bit of test code that illustrates the problem?


[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
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EasyMock, mixing class extenstion and normal mocking in same unit test
 
Similar Threads
Trying to Learn Unit Testing and Need Help
New to Mock Objects
Testing business object by mocking db interactions
Object argument does not work in method call on mock object
Unit Testing with Groovy?