aspose file tools*
The moose likes Testing and the fly likes problem with a Mock Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "problem with a Mock Object" Watch "problem with a Mock Object" New topic
Author

problem with a Mock Object

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
I have a Mock Object that implements Connection. I am trying to compile it using different versions (jdk 1.3 and jdk 1.4).

The problem is that in version 1.4, Connection includes methods that use a "Savepoint" (a new interface only found in v1.4). If I implement those methods, I can compile using v1.4 but not v1.3 (since 1.3 doesn't have a Savepoint interface). If I don't implement those methods, I can compile using 1.3 but not 1.4 because the class must be declared abstract since all the methods of the interface haven't been implemented.

Is there any way to resolve this and allow compilation in both versions?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Unfortunately there is no way around this... you will need to keep two copies of the class (one for each version). At build time, depending on the version of Java detected, Ant could build and include the appropriate class in the distribution.
More over, have you seen the MockObjects Project? It provides many different mock objects, ranging from JDBC to JMS (they provide different distributions depending on jdk and servlet version). Give it a look, maybe you are reinventing the wheel here...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In fact, there is a way around this - by using the dynamic proxy pattern (that is, creating an implementation to an interface at runtime through the java.lang.reflect.Proxy class).
http://www.easymock.org/ is providing a mock framework based on this pattern. It's quite powerfull, though for complex mocks it might become rather unhandy.
Hope this helps...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Just remember a different solution we used successfully: For JDK 1.3, just provide your own SavePoint interface!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Ilja Preuss:
Just remember a different solution we used successfully: For JDK 1.3, just provide your own SavePoint interface!

I tried that one, but then I get an error about the Connection method returning the wrong thing. (I can give you the exact error later, when I'm at home).
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
You would have to put your Savepoint interface in the java.sql package. Which of course is illegal according to the JLS but once you get your mock Connection compiled than it shouldn't matter much...
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Ilja Preuss:
[QB]In fact, there is a way around this - by using the dynamic proxy pattern (that is, creating an implementation to an interface at runtime through the java.lang.reflect.Proxy class).

That was very useful, Ilja. Thank you.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You're welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with a Mock Object