-4

I want to mock final method of a class. I tried

PowerMockito.mock(mockedClass.class)

But it is not working for me. It is giving NullPointerException. I am using TestNG to run my test cases.

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
  • Duplication of https://stackoverflow.com/questions/3793791/final-method-mocking – Oleg Cherednik Nov 29 '17 at 06:56
  • 1
    Just a little detail: PowerMockito helps you solve problems that you should not have. It's kind of like putting perfume on something that stinks. You might not smell it anymore, but the underlying problem is still there. Sometimes that's life, especially with less-than-well designed external libraries, but if you can do it another way, chances are, that way is better. – Florian Schaetz Nov 29 '17 at 07:06
  • 1
    Possible duplicate of [Final method mocking](https://stackoverflow.com/questions/3793791/final-method-mocking) – David Rawson Nov 29 '17 at 19:53

1 Answers1

4

According to Mockito documentation on final types and methods here it is possible to enable mocking of final classes and final methods by use the following dependency mockito-inline instead of mockito-core. I tried to do that and succeeded in mocking both a final class and a final method even without PowerMockito.

I used the following dependency

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-inline</artifactId>
        <version>2.12.0</version>
        <scope>test</scope>
    </dependency>

and ran tests using JDK 8.

Perhaps that may help you.

CDV
  • 106
  • 3