-1

In my project there is requirement where in I need to mock final class.

I am using Test-NG for unit tests.

I tried different things to mock but failed to do so. I also checked different stack overflow posts.

When running testng test case I am getting below error -

java.lang.IllegalArgumentException: Cannot subclass final class class com.AskStateParams
at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:110)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)

In pom.xml am using below dependencies -

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8.8</version>
  <optional>true</optional>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.9.5</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-mockito-release-full</artifactId>
  <version>1.5.1</version>
  <classifier>full</classifier>
</dependency>

TestNG code is as below -

@RunWith(PowerMockRunner.class)
@PrepareForTest(AskStateParams.class)
public class TestProcessor17100AAskAmount extends AbstractPowerMockProcessorTest {
  @Test
  public void test() {
    AskStateRequestParams  askParams=PowerMockito.mock(AskStateRequestParams.class);

    PowerMockito.when(askParams.getCallerInput()).thenReturn("1000");
    PowerMockito.when(askParams.getResultCode()).thenReturn("Y");
    PowerMockito.when(askParams.getMiString()).thenReturn("P=B");

    Assert.assertEquals(baseProcessor.postProcess(requestContext), "amount");
  }
}
Artur Zagretdinov
  • 2,034
  • 13
  • 22
GauravP
  • 81
  • 3
  • 14
  • What is AbstractPowerMockProcessorTest ? Could you share it ? Did you read https://github.com/jayway/powermock/wiki/TestNG_usage ? I think the object factory is missing. – juherr Mar 04 '16 at 06:07
  • Error itself shows that, A final class cannot be subclassed in JAVA.I think somewhere in your code you are trying to extend Final class. Its not testng specific issue. – Amit Bhoraniya Mar 04 '16 at 08:04
  • Final class is business requirement In order to test I have to mock that final class using PowerMockito.mock which am failing to do. AbstractPowerMockProcessorTest is simply a class where am mocking application specific classes for testing purpose for reusability in different tests. The issue is anyhow I need to mock final class. – GauravP Mar 04 '16 at 08:28
  • Possible duplicate: http://stackoverflow.com/q/14292863/3255152 – mfulton26 Mar 04 '16 at 13:13
  • @GauravP you might find the following helpful: https://objectpartners.com/2012/03/15/how-to-mock-final-classes-in-unit-tests/ – mfulton26 Mar 04 '16 at 13:18
  • @amitbhoraniya, yeah, you're right that **final** classes cannot be extended, so _cglib_ cannot create mock proxy for final class. But PowerMock can circumvent restrictions, by using custom class loader and removing **final** flag. And after that class can be mocked. But to use PowerMock class loader the `org.powermock.modules.testng.PowerMockObjectFactory`should be used with TestNG. – Artur Zagretdinov Mar 04 '16 at 19:31

1 Answers1

0

First of all, if you use TestNG, I wonder why you have @RunWith(PowerMockRunner.class). It's part of jUnit module and integration with jUnit, not TestNG. Please, look at TestNG usage on PowerMock wiki.

Second mistake, I don't see in your code mockStatic(AskStateParams.class). Again, please, check documentation.

Artur Zagretdinov
  • 2,034
  • 13
  • 22