Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

final class with inner static class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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: 40200
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic