(六)JMockit的API:Expectations--基础篇

Expectations的作用主要是:用于录制,即录制类、对象的调用,返回值是什么。

  1. 录制脚本规范
new Expectations() {
        // 这是一个Expectations匿名内部类
        {
        /*
        * 这是这个内部类的初始化代码块,我们在这里写录制脚本,脚本的格式要遵循下面的约定
        * 方法调用(可是类的静态方法调用,也可以是对象的非静态方法调用)
        * result赋值要紧跟在方法调用后面
        * ...其它准备录制脚本的代码
        * 方法调用
        * result赋值
        * */
        }
        };

        // 还可以再写new一个Expectations,只要出现在重放阶段之前均有效。
        new Expectations() {
        {
        //...录制脚本
        }
        };

Expectations主要有两种使用方式。

  1. 通过引用外部类的Mock对象(@Injectabe,@Mocked,@Capturing)来录制
//Expectations对外部类的mock对象进行录制
public class ExpectationsTest {
    @Mocked
    Calendar cal;
 
    @Test
    public void testRecordOutside() {
        new Expectations() {
            {
                cal.get(Calendar.YEAR);          // 对cal.get方法进行录制,并匹配参数 Calendar.YEAR
                result = 2016;                   // 年份不再返回当前小时。而是返回2016年
                
                cal.get(Calendar.HOUR_OF_DAY);   // 对cal.get方法进行录制,并匹配参数 Calendar.HOUR_OF_DAY
                result = 7;                      // 小时不再返回当前小时。而是返回早上7点钟
            }
        };
        Assert.assertTrue(cal.get(Calendar.YEAR) == 2016);
        Assert.assertTrue(cal.get(Calendar.HOUR_OF_DAY) == 7);
        // 因为没有录制过,所以这里月份返回默认值 0
        Assert.assertTrue(cal.get(Calendar.DAY_OF_MONTH) == 0);
    }
}

在这个例子中,在Expectations匿名内部类的初始代码块中,我们可以对外部类的任意成员变量,方法进行调用。大大便利我们书写录制脚本。

  1. 通过构建函数注入类/对象来录制–mock 类的某一个方法
    在上面的例子中,我们通过引用外部类的Mock对象(@Injectabe,@Mocked,@Capturing)来录制,可是无论是@Injectabe,@Mocked,@Capturing哪种Mock对象,都是对类的方法都mock了,可是有时候,我们只希望JMockit只mock类/对象的某一个方法。怎么办? 看下面的例子就明白啦。
//通过Expectations对其构造函数mock对象进行录制
public class ExpectationsConstructorTest2 {
 
    // 把类传入Expectations的构造函数
    @Test
    public void testRecordConstrutctor1() {
        Calendar cal = Calendar.getInstance();
        
        // 把待Mock的类传入Expectations的构造函数,可以达到只mock类的部分行为的目的
        new Expectations(Calendar.class) {
            {
                cal.get(Calendar.HOUR_OF_DAY);     // 只对get方法并且参数为Calendar.HOUR_OF_DAY进行录制
                result = 7;                       // 小时永远返回早上7点钟
            }
        };
        Calendar now = Calendar.getInstance();
        
        // 因为下面的调用mock过了,小时永远返回7点钟了
        Assert.assertTrue(now.get(Calendar.HOUR_OF_DAY) == 7);
        
        // 因为下面的调用没有mock过,所以方法的行为不受mock影响,
        Assert.assertTrue(now.get(Calendar.DAY_OF_MONTH) == (new Date()).getDate());
    }
 
    // 把对象传入Expectations的构造函数
    @Test
    public void testRecordConstrutctor2() {
        Calendar cal = Calendar.getInstance();
        // 把待Mock的对象传入Expectations的构造函数,可以达到只mock类的部分行为的目的,但只对这个对象影响
        new Expectations(cal) {
            {
                cal.get(Calendar.HOUR_OF_DAY);           // 只对get方法并且参数为Calendar.HOUR_OF_DAY进行录制
                result = 7;                             // 小时永远返回早上7点钟
            }
        };
 
        // 因为下面的调用mock过了,小时永远返回7点钟了
        Assert.assertTrue(cal.get(Calendar.HOUR_OF_DAY) == 7);
        
        // 因为下面的调用没有mock过,所以方法的行为不受mock影响,
        Assert.assertTrue(cal.get(Calendar.DAY_OF_MONTH) == (new Date()).getDate());
 
        // now是另一个对象,上面录制只对cal对象的影响,所以now的方法行为没有任何变化
        Calendar now = Calendar.getInstance();
        
        // 不受mock影响
        Assert.assertTrue(now.get(Calendar.HOUR_OF_DAY) == (new Date()).getHours());
        
        // 不受mock影响
        Assert.assertTrue(now.get(Calendar.DAY_OF_MONTH) == (new Date()).getDate());
    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值