jmockit中文网 expectations 入门

通过上面的例子,我们已经了解了Expectations的作用主要是用于录制。即录制类/对象的调用,返回值是什么。

录制脚本规范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
new  Expectations() {
     // 这是一个Expectations匿名内部类
     {
           // 这是这个内部类的初始化代码块,我们在这里写录制脚本,脚本的格式要遵循下面的约定
         //方法调用(可是类的静态方法调用,也可以是对象的非静态方法调用)
         //result赋值要紧跟在方法调用后面
         //...其它准备录制脚本的代码
         //方法调用
         //result赋值
     }
};
 
还可以再写 new 一个Expectations,只要出现在重放阶段之前均有效。
new  Expectations() {
      
     {
          //...录制脚本
     }
};

Expectations主要有两种使用方式。

    1. 通过引用外部类的Mock对象(@Injectabe,@Mocked,@Capturing)来录制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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对象(@Injectabe,@Mocked,@Capturing)来录制,可是无论是@Injectabe,@Mocked,@Capturing哪种Mock对象,都是对类的方法都mock了,可是有时候,我们只希望JMockit只mock类/对象的某一个方法。怎么办? 看下面的例子就明白啦。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//通过Expectations对其构造函数mock对象进行录制
public  class  ExpectationsConstructorTest2 {
 
     // 把类传入Expectations的构造函数
     @Test
     public  void  testRecordConstrutctor1() {
         Calendar cal = Calendar.getInstance();
         // 把待Mock的类传入Expectations的构造函数,可以达到只mock类的部分行为的目的
         new  Expectations(Calendar. class ) {
             {
                 // 只对get方法并且参数为Calendar.HOUR_OF_DAY进行录制
                 cal.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) {
             {
                 // 只对get方法并且参数为Calendar.HOUR_OF_DAY进行录制
                 cal.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());
     }
}

猜你喜欢

转载自www.cnblogs.com/funkboy/p/12012541.html