Powermock:尝试模拟静态时出现 NoClassDefFoundError 类
Powermock: NoClassDefFoundError when trying to mock static classes
我正在尝试了解如何使用 Powermock。
我正在尝试实现静态方法模拟 here.
的示例
我根据上面的例子创建了这段代码。
然而,我在尝试 运行 测试时收到 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();
}
}
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);
如上所述,
原因是版本兼容性。我正在使用 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 是目前最新的版本
我正在尝试了解如何使用 Powermock。 我正在尝试实现静态方法模拟 here.
的示例我根据上面的例子创建了这段代码。
然而,我在尝试 运行 测试时收到 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();
}
}
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);
如上所述, 原因是版本兼容性。我正在使用 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 是目前最新的版本