aspose file tools*
The moose likes Testing and the fly likes final class with inner static class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "final class with inner static class" Watch "final class with inner static class" New topic
Author

final class with inner static class

Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
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.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

I'd wrap it or put it in a mockable factory or the like.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final class with inner static class
 
Similar Threads
Method-Local Inner Class question
Please help
inner class
Inner Class
method overriding