PowerMockito:使用string []参数模拟静态方法时出现InvalidUseOfMatchersException

时间:2016-09-09 05:18:19

标签: java maven junit powermockito

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>

请让我知道你对这个问题的想法。

谢谢, Rajeswari

2 个答案:

答案 0 :(得分:1)

根据PowerMockito类的Javadoc,你应该写

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

答案 1 :(得分:0)

尝试使用以下详细信息创建一个新项目,请确认这是否与您的实现不同,但类名除外。因为这对我有用 -

基类 -

import java.util.ArrayList;

public class Shell {

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

测试类 -

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Shell.class)
public class ShellTest {

    @Test
    public void testExecuteShellCommand() {
        ArrayList resultData = new ArrayList();
        resultData.add("data1");
        resultData.add("data2");
        PowerMockito.mockStatic(Shell.class);
        PowerMockito.when(Shell.executeShellCommand(Mockito.any(String[].class))).thenReturn(resultData);
        System.out.println("All went fine.");
    }
}

<强> 的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>powermock-mockito</groupId>
    <artifactId>mock</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- 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>
        </dependency>
    </dependencies>

</project>