File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Engineering » Testing
Bookmark "final class with inner static class" Watch "final class with inner static class" New topic

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
author & internet detective

Joined: May 26, 2003
Posts: 33124

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

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, 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:
subject: final class with inner static class
It's not a secret anymore!