Search code examples
mavenjunitpowermockxstreamjboss-arquillian

Junit Test Error: No field 'segmentmask' found in class 'java.util.concurrent.ConcurrentHashMap'


Trying to run an Arquillian test with PowerMockRule in a Maven project to be able to mock static classes.

However, when I'm building the maven project, I get the following error in the test:

Tests in error: myTest(com.package.myTest): Could not call java.util.concurrent.ConcurrentHashMap.readObject() : No field 'segmentmask' found in class 'java.util.concurrent.ConcurrentHashMap'

I have no idea about the cause of this and how to go about fixing it. Any advice would be appreciated, thank you.

EDIT: Apparently the problem is caused by XStream class loader which is needed in using PowerMockRule. But I haven't found a fix.


Solution

  • This is indeed caused by an x-stream issue (which is fixed in 1.4.8): http://x-stream.github.io/jira/761/

    So if you use maven for dependency managing you might want to do something like this in your dependency management:

      <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-classloading-xstream</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
        <exclusions>
          <exclusion>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <!-- Should be removed as soon as xstream is updated in powermock -->
      <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.8</version>
        <scope>test</scope>
      </dependency>
    

    And then where you use powermock:

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-classloading-xstream</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Should be removed as soon as powermock has the latest version of xstream -->
    <dependency>
      <groupId>com.thoughtworks.xstream</groupId>
      <artifactId>xstream</artifactId>
      <scope>test</scope>
    </dependency>
    

    At least, until powermock starts to use the new x-stream version.