• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Mock a dependency that is static

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

first of all ,check the skeleton of classes




how could you think , i can mock the dependencies of BeingTested class in the test class.?

thankyou.
 
Sheriff
Posts: 4978
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a blog post a while back about solving this very problem. I'm talking about System.out.println in the article but you can apply the solution to any static calls.

http://www.timdrivendevelopment.com/observing-static-system-out-calls-in-tests/

Essentially you want to wrap the static calls in an instance class that can be replaced with a fake or mock.
 
ssiva raman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tim,

thanks for the reply.


that helped.but i want to do that ,unlike NameCaller , without changing the original source code.

do you have anyother idea?
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are not able to refactor the original source code to make it more testable, then take a look at PowerMock
It essentially rewrites the bytecode of your classes at runtime.  Because it does that it can mock things that normally can't be.

 
Tim Cooke
Sheriff
Posts: 4978
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it that you can't change the class under test? Or is it that you won't change it?

While quite unlikely, if you absolutely can't change the class under test then you either extend your definition of a Unit and test the dependency with the class, or use PowerMock as Stefan suggests.

If the dependency does not form a coherent unit with the class under test then what you have is a tight coupling problem, for which loosening the coupling is likely the better thing to do.
 
reply
    Bookmark Topic Watch Topic
  • New Topic