• 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

How do I design, if I need to access classes having private constructors and static methods?

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a scenario where I need to override the behavior of a class, but that class has a private default constructor (no argument constructor) and provide my own implemenation of a static method.

Assume that I have three classes A,B,C

class B and C has private no argument constructors. A invokes the static method of B, and in turn B invokes the static method of C. A has no direct visibility of C.

Now, I'm writing some unit tests for A and want to mock up the behavior of C. i.e ,I want to create a mock class for C, with my own implementation of the static method. For that I thought, I can over ride C and hide the implementation of a static method of the parent class, with my own. Similarly mock the behavior of B, hiding the implementation of the entire static method or part of it that, calling C. But my problem is I can not extend B or C to have my own implementation, due to private constructors. I could have implemented the interfaces which B and C are implementing, but I have to duplicate lot of code which is not needed for me and that would be a overkill.

Considering this scenario, is it possible to apply any design pattern to accomplish this, with out refactoring B or C. Any advice.

Thanks
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If those classes have all static methods, they can be called by ClassName.staticMethodName() and there is no problem.
reply
    Bookmark Topic Watch Topic
  • New Topic