0

我有最后一堂课

public final class A {
    private static final Set<String> B = methodA();

    private static Set<String> methodA() {
        //does some processing and 
        //returns a set 
    }

    public static boolean methodB() {
        //does some processing and 
        //returns a boolean 
    }
}

被测类是B类,这个类调用的是最终类A的静态方法

public class B {
    public boolean methodC() {
       if(methodB()) {
         C.methodD();
       }
    }
}

C班又是最后一班

public final class C {

    public static void methodD() {
        //does some processing
    }
}

我像这样嘲笑最后的A类

public class TestB {

    public void testMethodC() {
        PowerMockito.mock(A.class); 
    }
}

该案例在 C 类的方法 D 中失败。
我收到此错误Mockito 无法模拟此类:A。

4

1 回答 1

0

您需要一些注释:

@RunWith(PowerMockRunner.class)
@PrepareForTest({A.class})
public class TestB {

    public void testMethodC() {
        PowerMockito.mock(A.class); 
    }
}

我的依赖:

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>2.0.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>2.0.2</version>
    <scope>test</scope>
</dependency>
于 2020-02-07T08:18:14.533 回答