JMockit学习笔记

1 基本概念

1.1 常用注解

  • @Mocked:被修饰的对象将会被Mock,对应的类和实例都会受影响(同一个测试用例中)
  • @Injectable:仅Mock被修饰的对象
  • @Capturing:可以mock接口以及其所有的实现类
  • @Mock:MockUp模式中,指定被Fake的方法

1.2 常用的类

  • Expectations:期望,指定的方法必须被调用
  • StrictExpectations:严格的期望,指定方法必须按照顺序调用
  • NonStrictExpectations:非严格的期望,是否调用和顺序不作要求
  • Verifications:验证,一般配合NonStrictExpectations来使用
  • Invocation:工具类,可以获取调用信息
  • Delegate:自己指定返回值,适合那种需要参数决定返回值的场景,只需指定匿名子类就可以。
  • MockUp:模拟函数实现
  • Deencapsulation:反射工具类

2 使用实例

现有两个类如下:
SimpleTool.java:

public class SimpleTool {
    public String fun1(String str) {
        return "real: public String fun1(" + str + ")";
    }

    private String fun2(String str) {
        return "real: private String fun2(" + str + ")";
    }

    public String fun3(String str) {
        return "real: public String fun3(" + str + ")";
    }

    public String fun4(String str) {
        return fun2(str);
    }
}

UseSimpleTool.java:

public class UseSimpleTool {
    public String fun1(String name) {
        SimpleTool simpleTool = new SimpleTool();
        return simpleTool.fun1(name);
    }
}

2.1 Status-based

2.1.1 实例1:Mocked

@Mocked
SimpleTool simpleTool;

@Test
public void testExpectation() {

    //未mock函数返回null
    new Expectations() {
        {
            simpleTool.fun1(anyString);
            result = "MOCK";
        }
    };

    System.out.println(simpleTool.fun1("param"));
    System.out.println(simpleTool.fun3("param"));
    System.out.println(new UseSimpleTool().fun1("param"));

    new Verifications() {
       {
           simpleTool.fun1(anyString);
           times = 2;
       }
    };
}

输出:

MOCK
null
MOCK

2.1.2 实例2:Injectable

@Injectable
SimpleTool simpleTool;

@Test
public void testExpectation() {

    //未mock函数返回null
    new Expectations() {
        {
            simpleTool.fun1(anyString);
            result = "MOCK";
        }
    };

    System.out.println(simpleTool.fun1("param"));
    System.out.println(simpleTool.fun3("param"));
    System.out.println(new UseSimpleTool().fun1("param"));

    new Verifications() {
        {
            simpleTool.fun1(anyString);
            times = 1;
        }
    };
}

输出:

MOCK
null
real: public String fun1(param)

2.1.3 结果可变Delegate

@Mocked
SimpleTool simpleTool;

@Test
public void testExpectation() {

    new Expectations() {
        {
            simpleTool.fun1(anyString);
            result = new Delegate<String>() {
                public String aDelegateMethod(String str) {
                    return str.equals("param0") ? "MOCK0" : "MOCK1";
                }
            };
        }
    };

    System.out.println(simpleTool.fun1("param0"));
    System.out.println(simpleTool.fun3("param"));
    System.out.println(new UseSimpleTool().fun1("param1"));

    new Verifications() {
        {
            simpleTool.fun1(anyString);
            times = 2;
        }
    };
}

输出:

MOCK0
null
MOCK1

2.2 Behavior-based

2.2.1 Mock对象公有方法

@Test
public void testMockUp() {
    //影响该类所有实例
    new MockUp<SimpleTool>(){
        //未mock函数不受影响
        @Mock
        public String fun1(String str) {
            return "(MOCK)";
        }
    };

    SimpleTool simpleTool = new SimpleTool();
    System.out.println(simpleTool.fun1("param"));
    System.out.println(simpleTool.fun3("param"));
    UseSimpleTool useSimpleTool = new UseSimpleTool();
    System.out.println(useSimpleTool.fun1("param"));
}

输出:

(MOCK)
real: public String fun3(param)
(MOCK)

2.2.2 Mock对象私有方法

@Test
public void testMockUp() {
    new MockUp<SimpleTool>(){
        //未mock函数不受影响
        @Mock
        public String fun2(String str) {
            return "(MOCK)";
        }
    };

    SimpleTool simpleTool = new SimpleTool();
    System.out.println(simpleTool.fun1("param"));
    System.out.println(simpleTool.fun3("param"));
    System.out.println(simpleTool.fun4("param"));
}

输出:

real: public String fun1(param)
real: public String fun3(param)
(MOCK)

2.2.3 Mock接口

现有接口及其实现类如下:
SimleInterface.java:

public interface SimpleInterface {
    String getCityName();
    String getAreaName();
}

SimpleInterfaceImpl.java:

public class SimpleInterfaceImpl implements SimpleInterface {
    public String getCityName() {
        return "default city";
    }
    public String getAreaName() {
        return "default area";
    }
}

实例:

//此处使用Injectable和Mocked为一样的效果
@Injectable
SimpleInterface simpleInterface;

@Test
public void testUpper() throws Exception {
    SimpleInterface mockInstance =  new MockUp<SimpleInterface>() {
        @Mock
        public String getCityName() {
            return "BEIJING(MOCK)";
        }

        @Mock
        public String getAreaName() {
            return "HUABEI(MOCK)";
        }

    }.getMockInstance();

    System.out.println(mockInstance.getCityName());
    System.out.println(mockInstance.getAreaName());
    System.out.println(simpleInterface.getCityName());
    System.out.println(simpleInterface.getAreaName());
    SimpleInterfaceImpl simpleInterfaceImpl = new SimpleInterfaceImpl();
    System.out.println(simpleInterfaceImpl.getCityName());
    System.out.println(simpleInterfaceImpl.getAreaName());
}

输出:

BEIJING(MOCK)
HUABEI(MOCK)
null
null
default city
default area

对于接口的Mock如何影响实现该接口的所有类的对象,尚需进一步研究。

你可能感兴趣的:(测试,mock,jmockit)