如果 PowerMock 测试之前运行,则本地集成测试失败

Local integration test fails if a PowerMock test runs before

我已经在 Windows 上为 运行 编写了 HBase 本地集成测试,使用 HBaseTestingUtility 设置 HBase 的本地实例:

public class HBaseTestServer extends ExternalResource {
    private HBaseTestingUtility hbaseUtility;

    @Override
    protected void before() throws Exception {
        System.setProperty("test.build.data.basedirectory", "C:/Temp/hbase");
        this.hbaseUtility = new HBaseTestingUtility();
        this.hbaseUtility.startMiniCluster();
   }
   ...

如果我 运行 集成测试 class 本身就可以正常工作。然而,这个 class 运行s 在另一个测试 class 之后,它使用 PowerMock 模拟 HBase:

@RunWith(PowerMockRunner.class)
@PrepareForTest({HBaseAdapter.class, Connection.class, ConnectionFactory.class})
public class HBaseAdapterTest {
    ...

如果我 运行 对我的项目进行所有测试,首先是 PowerMock HBaseAdapterTest 运行,并且我的集成测试失败并显示错误:

java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
at org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Native Method)
at org.apache.hadoop.io.nativeio.NativeIO$Windows.access(NativeIO.java:606)
at org.apache.hadoop.fs.FileUtil.canWrite(FileUtil.java:977)

这是怎么回事?我想我需要在集成测试 运行 之前擦除所有 PowerMock 模拟,但我在网上找不到任何有用的东西。

对于任何对依赖关系感兴趣的人:

compile 'org.apache.hadoop:hadoop-client:2.8.1'
compile 'org.apache.hbase:hbase-client:1.1.2'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.8.0'
testCompile 'org.powermock:powermock-api-mockito2:1.7.0RC2'
testCompile 'org.powermock:powermock-module-junit4:1.7.0'
testCompile 'org.powermock:powermock-core:1.7.0'
testCompile 'org.powermock:powermock-module-junit4-rule:1.7.0'

根据 Tom 的评论,运行 在不同实例中的测试解决了我的问题。在 build.gradle 我添加了:

test {
    forkEvery 1
}