java静态类堆栈,java - Powermock:尝试模拟静态类时NoClassDefFoundError - 堆栈内存溢出...

我正在尝试了解如何使用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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值