Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

final class with inner static class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I submitted this question on the powermock google message board as well but figured I do this here too. So I have a 3rd party library I have no source for but no it is a final class with an inner static class with a public method. I have a class I am trying to write a test for that uses it. I am posting this code example to see if anyone has any ideas. It is a big abreviation from the actaul code but I believe I get all the important parts. Sadly I did not save my previous question so I have retype this whole thing.

Here is a skeleton of the 3rd party library:


Here is the class that uses it:



and here's the test


So I am trying to figure out how to mock new of a static inner class of a final class so I can mock its method call.
 
author & internet detective
Posts: 40359
820
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd wrap it or put it in a mockable factory or the like.
 
Michael Sampson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pat, I'd like to solve the puzzle please....
So I figured this out on my own. I think the static inner class muddled my thought on this. Even if a class is static, whenNew can still be used. However if you're using a mock of a final class then PowerMock.createMock needs to be used. So here is how I did this:
add the final class to the list of prepareForTest at the class level:

then in the testmethod:

anyObject being a hamcrest matcher. createMock, replay, and explect all are static imports from the PowerMock class. Sorry for not posting the import statements.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic