Powermock:当尝试模拟静态类时出现NoClassDefFoundError错误

3
我正在尝试理解如何使用Powermock。 我正在尝试实现静态方法模拟的示例,在这里。 我根据上述示例创建了此代码。 然而,在尝试运行测试时,我遇到了NoClassDefFoundError错误。 由于这基本上是复制粘贴的代码,我不知道究竟是什么导致了这个错误。
// imports redacted

@RunWith(PowerMockRunner.class)
@PrepareForTest(Static.class)
public class YourTestCase {
    @Test
    public void testMethodThatCallsStaticMethod() throws Exception {
        // mock all the static methods in a class called "Static"
        PowerMockito.mockStatic(Static.class);
        // use Mockito to set up your expectation
        PowerMockito.when(Static.class, "firstStaticMethod", any()).thenReturn(true);
        PowerMockito.when(Static.class, "secondStaticMethod", any()).thenReturn(321);

        // execute your test
        new ClassCallStaticMethodObj().execute();

        // Different from Mockito, always use PowerMockito.verifyStatic() first
        // to start verifying behavior
        PowerMockito.verifyStatic(Mockito.times(2));
        // IMPORTANT:  Call the static method you want to verify
        Static.firstStaticMethod(anyInt());


        // IMPORTANT: You need to call verifyStatic() per method verification,
        // so call verifyStatic() again
        PowerMockito.verifyStatic(); // default times is once
        // Again call the static method which is being verified
        Static.secondStaticMethod();

        // Again, remember to call verifyStatic()
        PowerMockito.verifyStatic(Mockito.never());
        // And again call the static method.
        Static.thirdStaticMethod();
    }
}

class Static {
    public static boolean firstStaticMethod(int foo) {
        return true;
    }

    public static int secondStaticMethod() {
        return 123;
    }

    public static void thirdStaticMethod() {
    }
}

class ClassCallStaticMethodObj {
    public void execute() {
        boolean foo = Static.firstStaticMethod(2);
        int bar = Static.secondStaticMethod();
    }
}
2个回答

6

PowerMock 1.6.6似乎与Mockito 2.7不兼容。

我对你的pom.xml进行了一些更改。首先,我更改了powermock版本:

<powermock.version>1.7.0RC2</powermock.version>

然后我将 powermock-api-mockito 更改为 powermock-api-mockito2 (第一个似乎与Mockito 2.7不兼容,因此无法使用):

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>${powermock.version}</version>
    <scope>test</scope>
</dependency>

这解决了NoClassDefFoundError问题。


无论如何,我还是必须做出改变才能使它工作:你应该使用Mockito.when()代替PowerMockito.when()

Mockito.when(Static.firstStaticMethod(anyInt())).thenReturn(true);
Mockito.when(Static.secondStaticMethod()).thenReturn(321);

1
遗憾的是,这并不能解决NoClassDefFoundError错误。 - Philippe
你的类路径设置如何? - user7605325
这是错误信息的链接。我认为问题出在WhiteboxImpl上。 - Philippe
我并没有显式地设置我的类路径。我只是有这个 pom.xml 文件并运行 mvn test。 - Philippe
1
谢谢@Hugo,但是在与同事交谈后,我决定采用更好的方法来模拟静态字段。因此,我将不使用powermock进行测试。 - Edison Spencer
显示剩余4条评论

0
如上所述, 原因是版本兼容性。我正在使用mockito 3(3.3.3) 通过以下方式声明powermockito的依赖项,解决了我的问题。
      <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>2.0.7</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito2</artifactId>
      <version>2.0.7</version>
      <scope>test</scope>
   </dependency>

目前2.0.7是最新版本


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接