gpt4 book ai didi

java - PowerMockito:使用字符串 [] 参数模拟静态方法时出现 InvalidUseOfMatchersException

转载 作者:行者123 更新时间:2023-11-30 10:39:08 27 4
gpt4 key购买 nike

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:在此处检测到错误的参数匹配器:

我在对以下静态方法执行 powermockito 时遇到了 org.mockito.exceptions.misusing.InvalidUseOfMatchersException 异常。

public class ShellCommandUtil {
public static ArrayList executeShellCommand(String[] shellcmd) {
ArrayList output = new ArrayList();
//removed the actual logic of sending shellcommand to the system getting the result;
return output;
}
}

编写的TestMethod定义如下

@RunWith(PowerMockRunner.class)
@PrepareForTest(ShellCommandUtil.class)
public class ShellDataTest{
@Test
public void testExecuteShellCommand() {
ArrayList resultData = new ArrayList();
resultData.add("data1");
resultData.add("data2");
PowerMockito.mockStatic(ShellCommandUtil.class);
PowerMockito.when(ShellCommandUtil.executeShellCommand(Mockito.any(String [].class))).thenReturn(resultData);
}
}

你能帮忙看看这一行定义的错误吗

PowerMockito.when(ShellCommandUtil.executeShellCommand(Mockito.any(String[].class))).thenReturn(resultData);

在pom.xml中,我添加了以下依赖

<!-- https://mvnrepository.com/artifact/org.powermock/powermock-mockito-release-full -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.6.4</version>
<type>pom</type>
</dependency>

请让我知道您对这个问题的看法。

谢谢,拉杰斯瓦里

最佳答案

根据PowerMockito类的Javadoc,你应该写

PowerMockito.when(ShellCommandUtil.class, "executeShellCommand", Mockito.any(String [].class)).thenReturn(resultData);

关于java - PowerMockito:使用字符串 [] 参数模拟静态方法时出现 InvalidUseOfMatchersException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39404309/

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