Error mocking final class with static method with JUnit 4.x and Mockito.

1,199 views
Skip to first unread message

abu0khalique

unread,
May 6, 2016, 12:45:29 AM5/6/16
to PowerMock
Hi,

I am having challenges stubbing a final class with static method for kafka.admin;AdminUtils. So i wanted to make sure final class static method can be stubbed.

I have copied a class called

public final class AFinalClass {

    public static final String echoString(String s) {
        return s;
    }
}


and a test 

@PrepareForTest({AFinalClass.class})
public class AFinalClassTest {

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();
    @Rule
    public PowerMockRule powerMockRule = new PowerMockRule();

    @Test
    public void mockFinalClassTest() {
        PowerMockito.mockStatic(AFinalClass.class);

        AFinalClass stub = mock(AFinalClass.class);

        final String testInput = "A test input";
        final String mockedResult = "Mocked final echo result - " + testInput;
        PowerMockito.when(stub.echoString(testInput)).thenReturn(mockedResult);

        // Assert the mocked result is returned from method call
        Assert.assertEquals(stub.echoString(testInput), mockedResult);
    }
}


but I get this error message

java.lang.IllegalArgumentException: Cannot subclass final class class org.song.example.AFinalClass

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.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:124)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:58)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:122)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMock(MockCreator.java:70)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:47)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:71)
at org.song.example.AFinalClassTest.mockFinalClassTest(AFinalClassTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:46)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:43)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:16)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:270)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:62)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:52)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:307)
at org.junit.runner.JUnitCore.run(JUnitCore.java:152)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


I am using

Java 1.8,  following gradle dependencies

    testCompile "junit:junit:4.11"
    testCompile 'org.hamcrest:hamcrest-core:1.3'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'com.github.stefanbirkner:system-rules:1.16.0'
    testCompile 'junit-addons:junit-addons:1.4'
    testCompile "org.mockito:mockito-all:2.0.2-beta"
    testCompile "org.powermock:powermock-module-junit4:1.6.5"
    testCompile "org.powermock:powermock-api-mockito:1.6.5"
    testCompile "org.powermock:powermock-module-junit4-rule-agent:1.6.5"

Johan Haleby

unread,
May 6, 2016, 12:47:35 AM5/6/16
to powe...@googlegroups.com
Hi,

It might be because you're using the JUnit rule which is (still) experimental. If possible I suggest that you use the PowerMockRunner.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at https://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

abu0khalique

unread,
May 6, 2016, 2:14:14 PM5/6/16
to PowerMock
Thanks it works without rules

@RunWith(PowerMockRunner.class)
@PrepareForTest({AFinalClass.class})
public class AFinalClassTest {

    //@Rule
    //public MockitoRule mockitoRule = MockitoJUnit.rule();
    //@Rule
    //public PowerMockRule powerMockRule = new PowerMockRule();

    @Test
    public void mockFinalClassTest() {
        PowerMockito.mockStatic(AFinalClass.class);

        final String testInput = "A test input";
        final String mockedResult = "Mocked final echo result - " + testInput;
        PowerMockito.when(AFinalClass.echoString(testInput)).thenReturn(mockedResult);

        // Assert the mocked result is returned from method call
        Assert.assertEquals(AFinalClass.echoString(testInput), mockedResult);
Reply all
Reply to author
Forward
0 new messages