JMockit实践--(3)Expectations

来源:互联网 发布:怎么样开淘宝网店 编辑:程序博客网 时间:2024/06/09 17:56

声明:

Author:赵志乾

Date:2017-8-6

Declaration:All Right Reserved!!!



    1、在给定的测试中,一个expectation代表对一个特定的、被mocked的方法/构造函数的一组调用。它可能会涵盖测试过程中该方法的多个不同的调用,但这并非强制要求。而方法的一次调用是否匹配特定的expectation,不仅取决于方法签名,还包括传入参数、以及运行时已经匹配的调用次数。我们可以通过指定特定的参数值,使得特定的函数调用匹配特定的expectation。当然,也可以通过指定比较宽泛的匹配条件,使得同一方法的一组调用均能匹配该expectation。下面给出一个示例:

@Testpublic void doBusinessOperationXyz(@Mocked final Dependency mockInstance){   ...   new Expectations() {{      ...      // An expectation for an instance method:      mockInstance.someMethod(1, "test"); result = "mocked";      ...   }};   // A call to code under test occurs here, leading to mock invocations   // that may or may not match specified expectations.}

    该示例给出的是Dependency#someMethod(int, String)方法的expectation,并且需要通过特定的参数匹配方法调用。

2、录制、重放、验证三阶段

    测试过程通常可以划分出三个顺序执行的阶段,如下例所示:

@Testpublic void someTestMethod(){   // 1. Preparation: whatever is required before the code under test can be exercised.   ...   // 2. The code under test is exercised, usually by calling a public method.   ...   // 3. Verification: whatever needs to be checked to make sure the code exercised by   //    the test did its job.   ...}

    首先是准备阶段,在该阶段主要是创建/获取测试过程中需要的对象和数据项。之后是在测试条件下执行代码。最后是结果的验证,即将执行结果和预期结果进行对比。而在基于行为测试(即包含mocked类型)的上下文中,我们通常会将测试过程划分为下面的三个阶段。

    首先是录制阶段,其主要是在mocked方法被调之前对其调用行为进行录制,即完成测试的准备工作。其次是重放阶段,该阶段是指代码执行过程中,如果mocked的方法被调用,将会重放前一阶段录制的行为,即mocked方法表现出预期的行为。最后是验证阶段,该阶段是在测试验证过程中发生,主要是验证mocked方法的调用和我们预期的是否一致。下面给出了使用JMockit的基于行为的测试模板:

import mockit.*;... other imports ...public class SomeTest{   // Zero or more "mock fields" common to all test methods in the class:   @Mocked Collaborator mockCollaborator;   @Mocked AnotherDependency anotherDependency;   ...   @Test   public void testWithRecordAndReplayOnly(mock parameters)   {      // Preparation code not specific to JMockit, if any.      new Expectations() {{ // an "expectation block"         // One or more invocations to mocked types, causing expectations to be recorded.         // Invocations to non-mocked types are also allowed anywhere inside this block         // (though not recommended).      }};      // Unit under test is exercised.      // Verification code (JUnit/TestNG assertions), if any.   }   @Test   public void testWithReplayAndVerifyOnly(mock parameters)   {      // Preparation code not specific to JMockit, if any.      // Unit under test is exercised.      new Verifications() {{ // a "verification block"         // One or more invocations to mocked types, causing expectations to be verified.         // Invocations to non-mocked types are also allowed anywhere inside this block         // (though not recommended).      }};      // Additional verification code, if any, either here or before the verification block.   }   @Test   public void testWithBothRecordAndVerify(mock parameters)   {      // Preparation code not specific to JMockit, if any.      new Expectations() {{         // One or more invocations to mocked types, causing expectations to be recorded.      }};      // Unit under test is exercised.      new VerificationsInOrder() {{ // an ordered verification block         // One or more invocations to mocked types, causing expectations to be verified         // in the specified order.      }};      // Additional verification code, if any, either here or before the verification block.   }}

    注意:一个测试可以包含任意数量的expectation块,甚至可以不包含。

    事实上,匿名内部类可以定界代码块,这可以让我们充分利用IDE中代码折叠的特性,如下面的这张图片所示:



注:本博客中的实例代码均来自于JMockit官方教程。




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑qq群视频进不去怎么办 微信头像没保存怎么办 qq群信息不响怎么办 微信封面换不了怎么办 微信头像没有了怎么办 科二超过5次怎么办 蘑菇街个人中心打不开怎么办 蘑菇街的个人中心打不开怎么办 qq群500人满了怎么办 微信零钱发不出来怎么办 微信零钱发不出去怎么办 微信零钱发不了红包怎么办 当亲戚孤立你该怎么办 陌陌好友找不到了怎么办 换了手机号陌陌号找不到怎么办 微信解析包出现问题怎么办 老板找借口不发工资怎么办 苹果手机忘记id帐号密码怎么办 苹果6手机帐号密码忘记怎么办 狗狗在窝里撒尿怎么办 爸妈和媳妇不好怎么办 如果老婆不用老公的钱怎么办 老公不给老婆钱花怎么办 奶少孩子不愿意吸怎么办 孩子识字少不愿意读书怎么办 夏天孩子不愿意喝水尿量少黄怎么办 孩子不喜欢与别人沟通怎么办 异地恋他爸妈不同意怎么办 刚装修家里很臭怎么办 狗弄得家里很臭怎么办 抑郁症患者不愿意回家见家人怎么办 属猴的人养猫怎么办 对方电话拉黑我怎么办 手机号码换了淘宝账号怎么办 扣扣号有密码无法加好友怎么办 扣扣帐号忘记了怎么办 扣扣帐号被冻结怎么办 xp忘了开机密码怎么办 手机怎样知道好友qq密码怎么办 找回微信密码申诉失败怎么办 若微信号被盗钱怎么办