Java 在不运行Junit的情况下使用PowerMock模拟类

Java 在不运行Junit的情况下使用PowerMock模拟类,java,unit-testing,junit,powermock,Java,Unit Testing,Junit,Powermock,B的实现是这样的 @Runwith(PowerMockRunner.class) @PrepareForTest(Test.class) public class A { public static void main(String []args){ PowerMockito.mockStatic(Test.class); when(Test.foo()).thenReturn(true); B.foo(); } } 我正在编写代

B的实现是这样的

@Runwith(PowerMockRunner.class)
@PrepareForTest(Test.class)
public class A {
    public static void main(String []args){
        PowerMockito.mockStatic(Test.class);
        when(Test.foo()).thenReturn(true);
        B.foo();
    }
}
我正在编写代码来测试
B.foo()
它调用
test.foo()
我想模拟
test.foo()
以始终返回
true
。但是当我运行这段代码时,当()需要一个参数时,它产生了一个错误
,该参数必须是“模拟的方法调用”
。我没有编辑类别B或类别
测试的权限。如果我将类A中的方法设置为Junit,那么它可以正常工作。但是我不能像Junit那样运行它,因为我在大量输入上运行它。有没有办法在没有Junit的情况下模拟一个方法

两个问题:

  • 你要求考试是为了准备考级,而不是B级
  • 您正在使用mockito和powermock?你应该使用

    expect(Test.foo()).andReturn(true)

而不是

public class B{
    public static void foo(){
        boolean f = Test.foo();
    }
}
完成后,您应该调用replay,如下所示:

when(Test.foo()).thenReturn(true);

如果答案对你有用,你介意接受它(并给予应有的信任)吗?它没有解决问题
replay(Test.class);