Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to use Spy class in PowerMockito for Final class which has private Consturctor ?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator






For the  Above Code in Junit Test Case i need to use spy.

Spy need new Instance. But Main Class is Final Class as well it is private constructor so using reflection i created instance but if i pass that instance inside it throwing eror

" Cannot subclass final class "

Please Help me on this.
 
Marshal
Posts: 5321
324
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
Normally when I start to think "oh PowerMock would help me out with this test" I take that as a massive alarm bell that there's likely something wrong with my design or that I'm trying to test the wrong thing.

In this example the class MainClass is completely useless as you can't instantiate it through normal programming, i.e. without hoking behind the scenes with reflection. So, if the class cannot be used then why is it there at all?
 
jerald muthu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Tim but if Main Class containing static Methods.
 
Sheriff
Posts: 16919
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you gave as an example does not provide a good basis for a useful discussion. It's like talking about what you'd need to do to be able to kick yourself in the head: first, you'd need to be some kind of contortionist to pull it off and second, why?!!! That's pretty much the thing I see with what you seem to be trying to do there with that code, to be honest.
 
jerald muthu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu Lacar,

In MainClass all the methods are Static i need to do Junit for Code Coverage. i completed almost but for Exception i need to do junit code cover.. Above code i have created similar to my utillity class.

Now Please Help on this to cover code coverage... What i need to ...
 
Tim Cooke
Marshal
Posts: 5321
324
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
It's really simple. You write tests that exercise the functionality of the methods. No need to use any mocking frameworks for this, just run the real code.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic