JMockit:使用 Expectations 时候如何进行参数匹配


#Java Jmockit#


示例

测试类

package demo;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

public class EchoUtil {

    public static String echo(int i) {
        return "输入的int为 " + i;
    }

    public static String echo(String s) {
        return "输入的String为 " + s;
    }

    public static String echo(AtomicBoolean v) {
        return "输入的AtomicBoolean为 " + v;
    }

    public static String echo(AtomicLong v) {
        return "输入的AtomicLong为 " + v;
    }
}

明确的值匹配

package demo;


import mockit.Expectations;
import org.junit.Test;

public class EchoUtilTest {

    @Test
    public void test() {
        new Expectations(EchoUtil.class) {{
            EchoUtil.echo(1);
            result = "你好 int";
            EchoUtil.echo("s1");
            result = "你好,字符串";
        }};

        System.out.println(EchoUtil.echo(1));  // 你好 int
        System.out.println(EchoUtil.echo("s1"));  // 你好,字符串

        System.out.println(EchoUtil.echo(2));  // 输入的int为 2
        System.out.println(EchoUtil.echo("s2"));  // 输入的String为 s2
    }

}

使用 anyXX 匹配任意值

可以使用 anyInt 匹配任意 int 或者 Integer 。anyLong、anyShort、anyChar、anyByte、anyBoolean、anyDouble、anyFloat 类似。

anyString 可以匹配任意 String 。

package demo;


import mockit.Expectations;
import org.junit.Test;

public class EchoUtilTest {

    @Test
    public void test() {
        new Expectations(EchoUtil.class) {{
            EchoUtil.echo(anyInt);
            result = "你好 int";
            EchoUtil.echo(anyString);
            result = "你好,字符串";
        }};

        System.out.println(EchoUtil.echo(1));  // 你好 int
        System.out.println(EchoUtil.echo(2));  // 你好 int
        System.out.println(EchoUtil.echo("s1"));  // 你好,字符串
        System.out.println(EchoUtil.echo("s2"));  // 你好,字符串
    }

}

使用 any 匹配任意对象

要加一层类型转换。

package demo;


import mockit.Expectations;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicLong;

public class EchoUtilTest {

    @Test
    public void test() {
        new Expectations(EchoUtil.class) {{
            // EchoUtil.echo((Integer)any);  // 这里会报错 NPE ,
            // result = "你好 int";
            EchoUtil.echo((String)any);
            result = "你好,字符串";
            EchoUtil.echo((AtomicLong)any);
            result = "你好 AtomicLong";
        }};

        System.out.println(EchoUtil.echo(1));  // 输入的int为 1
        System.out.println(EchoUtil.echo(2));  // 输入的int为 2
        System.out.println(EchoUtil.echo("s1"));  // 你好,字符串
        System.out.println(EchoUtil.echo("s2"));  // 你好,字符串
        System.out.println(EchoUtil.echo(new AtomicLong()));  // 你好 AtomicLong
    }

}

为什么 EchoUtil.echo((Integer)any) 会报错?因为 echo 方法中没有 Integer 类型的参数,只有 int。若 String echo(int i) 改为 String echo(Integer i) 则不会报错。

使用 withXX 匹配

with 开头的匹配方式有很多,例如 withInstanceOf 。

package demo;


import mockit.Expectations;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicLong;

public class EchoUtilTest {

    @Test
    public void test() {
        new Expectations(EchoUtil.class) {{
            EchoUtil.echo(withInstanceOf(String.class));
            result = "你好,字符串";
            EchoUtil.echo(withInstanceOf(AtomicLong.class));
            result = "你好 AtomicLong";
        }};


        System.out.println(EchoUtil.echo("s1"));  // 你好,字符串
        System.out.println(EchoUtil.echo("s2"));  // 你好,字符串
        System.out.println(EchoUtil.echo(new AtomicLong()));  // 你好 AtomicLong
    }

}


( 本文完 )