Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PowerMockito正在调用实方法而不是模拟的私有方法_Java_Unit Testing_Junit_Mockito_Powermockito - Fatal编程技术网

Java PowerMockito正在调用实方法而不是模拟的私有方法

Java PowerMockito正在调用实方法而不是模拟的私有方法,java,unit-testing,junit,mockito,powermockito,Java,Unit Testing,Junit,Mockito,Powermockito,我有一个类,它有一个私有方法,并在其主体中调用另一个私有方法。所以我想调用第一个私有方法并模拟第二个。以下是一个例子: public class ClassWithPrivateMethod { private int count; public ClassWithPrivateMethod() { } private void countDown() { while (isDecrementable()) count

我有一个类,它有一个私有方法,并在其主体中调用另一个私有方法。所以我想调用第一个私有方法并模拟第二个。以下是一个例子:

public class ClassWithPrivateMethod {

    private int count;

    public ClassWithPrivateMethod() {
    }

    private void countDown() {
        while (isDecrementable())
            count--;
    }

    private boolean isDecrementable() {

        throw new IllegalArgumentException();
//        if (count > 0) return true;
//        return false;
    }
}
和测试等级:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithPrivateMethodTest.class)
public class ClassWithPrivateMethodTest {

    private int count;
    private ClassWithPrivateMethod classWithPrivateMethod;

    @Before
    public void setUp() throws Exception {
        count = 3;
        classWithPrivateMethod = PowerMockito.spy(new ClassWithPrivateMethod());
    }

    @Test
    public void countDown() throws Exception {

        Whitebox.setInternalState(classWithPrivateMethod, "count", count);
        PowerMockito.doReturn(true, true, false).when(classWithPrivateMethod, "isDecrementable");

        Whitebox.invokeMethod(classWithPrivateMethod, "countDown");

        int count = Whitebox.getInternalState(classWithPrivateMethod, "count");

        assertEquals(1, count);

        PowerMockito.verifyPrivate(classWithPrivateMethod, times(3)).invoke("isDecrementable");
    }
}
我通过Whitebox.invokeMethod(classWithPrivateMethod,“倒计时”)调用了第一个私有方法和第二个类似的模拟,
PowerMockito.doReturn(true,true,false).when(classWithPrivateMethod,“isDecrementable”)。如您所见,
isDecrementable
方法在调用时应返回
true
true
false
值,但PowerMockito正在调用real方法。

我正在使用这些依赖项:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.5</version>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.19</version>
</dependency>

朱尼特
朱尼特
4.12
org.powermock
powermock api mockito
1.6.5
org.powermock
powermock-module-junit4
1.6.5
测试
org.mockito
莫基托磁芯
1.10.19

我的测试有什么问题?我发现这行有错误:

@PrepareForTest(ClassWithPrivateMethodTest.class)
应该是:

@PrepareForTest(ClassWithPrivateMethod.class)