gpt4 book ai didi

java - Powermockito verifystatic 问题

转载 作者:行者123 更新时间:2023-11-30 07:50:32 28 4
gpt4 key购买 nike

我有一个遗留类,它有一个我需要测试的静态 void 方法:

public class A {

public static void renameTo()
{
String ext = "." + this.fileName + ".backup";
for (File file : getCSVFile()) {
f.renameTo(new File(file.getAbsolutePath() + ext));
}

public static File[] getAllFiles()
{
//logic to read the CSV files from the class path
}


}

现在我已经使用 PowerMockito 为它编写了一个测试用例,如下所示。现在的问题是,即使只调用了 renameTo(),如果我调用 PowerMockito.verifyStatic( Mockito.times(10)) 测试仍然通过

@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
@PowerMockIgnore("javax.management.*")
public class ATest {

@Test
public void testRenameTo() throws Exception {
PowerMockito.mockStatic(A.class);
A.renameTo();
PowerMockito.verifyStatic( Mockito.times(1));
//PowerMockito.verifyStatic( Mockito.times(5));//Passes even though the mehod is called only once
//PowerMockito.verifyStatic( Mockito.times(10);//Passes even though the mehod is called only once
}
}

有人可以阐明这个问题吗?我可能做错了什么?

最佳答案

根据 documentation ,测试后需要先调用verifyStatic,然后调用A.renameTo()告诉它验证哪个静态方法。示例:

// run test
A.renameTo();

// verify interaction
PowerMockito.verifyStatic(A.class, Mockito.times(1));
A.renameTo();

关于java - Powermockito verifystatic 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47760899/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com