Mockito latest versions support to surpress static block

Viewed 2070

I am trying to add tests using latest (5.7.0) Mockito and using Mockito.mockStatic(...) to mock class with static methods which works fine . However when I have class with static block it is unable to create mock and fais with java.lang.InternalError: class redefinition failed: invalid class .

Is this supported in latest mockito versions or still I have to live with other alternatives like powermock.

1 Answers

I did face the same error and couldn't find answer in the internet, so I see that it is 1 month old question but might help someone.

The problem was that mocked class (or classes that this static block used) was not in the classpath. Before you can mock the method all static blocks or fields initializators will be run, and it causes the problem.

You can verify what class is missing in your classpath by simply trying to use this static method before mocking it. You should get java.lang.NoClassDefFoundError with a class name.

When you resolve this problem, everything should work.

Related