Cannot subclass final class

3,329 views
Skip to first unread message

mike

unread,
Sep 17, 2013, 8:30:08 AM9/17/13
to powe...@googlegroups.com
Hi,

We try to run a testcase using powermockito and testng but it fails with:

java.lang.IllegalArgumentException: Cannot subclass final class class <companypackage>.ResourceElement

We have the following versions of powermock ( mockito):


      <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-api-mockito</artifactId>
      <version>1.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-testng</artifactId>
      <version>1.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.8</version>
      <scope>test</scope>
    </dependency>


And we try the following testcase:


import static org.fest.assertions.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import <comapanypackage>.ResourceElement;


@PrepareForTest(MyFinalClass.class)
public class ResourceTest extends PowerMockTestCase {

    private static ResourceElement xmlMock;

  

    @BeforeMethod
    public static void beforeMethod() throws Exception {

        //xmlMock = mock(ResourceElement.class);
      
      
    }

  
The ResourceElement is a final class:

looks like ( not all code)

public final class ResourceElement {

ResourceElement(Resource resource, TypeMapper mapper) {
        this.resource = resource;
        this.mapper = mapper;
    }

}

Resource is an abstract class and TypeMapper is a final ( not public but default package) class.

We managed to run this if we use Junit but not with TestNG.
Any ideas?

br,

//mike

  

mike

unread,
Sep 17, 2013, 8:40:06 AM9/17/13
to powe...@googlegroups.com
I tried to use instead of extending PowerMockTestCase  but I cannot find PowerMockRunner in my classpath (Eclipse).

br,

//mike

@RunWith(PowerMockRunner.class);

mike

unread,
Sep 17, 2013, 8:54:51 AM9/17/13
to powe...@googlegroups.com
I used


and found PowerMockRunner.class but still the same problem.

java.lang.
IllegalArgumentException: Cannot subclass final class class <companypackage>.ResourceElement

br,

//mike

mike

unread,
Sep 17, 2013, 8:58:24 AM9/17/13
to powe...@googlegroups.com
I run the testcase with VM arg:-XX:-UseSplitVerifier otherwise I get:

nconsistent stackmap frames at branch target 38 in method ....

//mike

Johan Haleby

unread,
Sep 17, 2013, 9:31:54 AM9/17/13
to powe...@googlegroups.com
You don't have a RunWith annotation in TestNG so you cannot use that. If you use TestNG it's better if you extend from PowerMockTestCase (it seem to resolve some issues). Perhaps the problem can be that you create the mock in the @beforeMethod, what happens if you move it in to a test method? Try also making the method non-static and see if that makes any difference.

/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 http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/groups/opt_out.

mike

unread,
Sep 20, 2013, 7:57:31 AM9/20/13
to powe...@googlegroups.com
I tried removing static in beforeMethod and it worked. I am not sure why it works. Do you ?

br,

//mike

Johan Haleby

unread,
Sep 21, 2013, 8:00:18 AM9/21/13
to powe...@googlegroups.com
Don't think PowerMock has support for creating mocks in those methods.


mike

unread,
Oct 7, 2013, 6:44:23 AM10/7/13
to powe...@googlegroups.com
Where can I find information about that.

//mike

Johan Haleby

unread,
Oct 8, 2013, 2:23:40 AM10/8/13
to powe...@googlegroups.com
I've added documentation for it here.

/Johan
Reply all
Reply to author
Forward
0 new messages