mocking a class that has a private constructor?

3.4k views Asked by At

I need to mock a java class that has a private constructor and only a static create method.

What would be the typical solution in that kind of situation.

With or without a framework.

1

There are 1 answers

3
Jeff Storey On

You can use PowerMock to mock static methods https://code.google.com/p/powermock/wiki/MockitoUsage, so you would mock the static create method and return whatever you want. It's easier if the static method returns an interface rather than a class so you can return your own mock implementation of it.