• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with a Mock Object

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remember a different solution we used successfully: For JDK 1.3, just provide your own SavePoint interface!
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic