This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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: 4352
280
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
yes Tim but if Main Class containing static Methods.
 
Sheriff
Posts: 11745
191
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4352
280
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!