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

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

尝试 运行 在 Maven 项目中使用 PowerMockRule 进行 Arquillian 测试,以便能够模拟静态 classes。

但是,我在构建maven项目时,在测试中出现如下错误:

错误测试: myTest(com.package.myTest): 无法调用 java.util.concurrent.ConcurrentHashMap.readObject(): 在 class 'java.util.concurrent.ConcurrentHashMap'

中找不到字段 'segmentmask'

我不知道造成这种情况的原因以及如何解决它。如有任何建议,我们将不胜感激。

编辑:显然问题是由 XStream class 加载器引起的,它是使用 PowerMockRule 所必需的。但是我还没有找到解决方法。

这确实是由x-stream问题引起的(已在1.4.8中修复): http://x-stream.github.io/jira/761/

所以如果你使用 maven 进行依赖管理,你可能想在你的依赖管理中做这样的事情:

  <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>

然后是你使用 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>

至少,直到 powermock 开始使用新的 x-stream 版本。