Mock Framework For Unit Test

Why we need Unit Test

Unit Test executes your logic code with known input parameters and evaluates the output for expected results.
Different from the Integration Test And Functional Test. They are only testing the isolation method. Focused on the smallest possible bit of code that can be tested. Unit Test ahead of integration testing. And make sure the logic code can running successfully based on the test data. Discover problems in advance. Another thing your code average coverage should above 75%.

How to write Unit Test

Positive Unit Test Step

Negative Unit Test Step
  • Prepare Test Data

    1. Unit Test Data is temporary. When you Unit Test finished. And Data will rollback
    2. Your Test Data shouldn't rely on Organization Data.
    3. Different Unit Test should have their own-self Test Data
    4. You can prepare Test Data by Apex Operation or Static Source
  • Call Test.StartTest() And Test.StopTest()
    This resets governor limits and isolates your test from the test setup.
    Execute the code you’re testing.

  • Make assertions about the output of your tested code. You might assert a record exists, or that a field is set to an expected value.

Write Unit Test Step

  1. @isTest Annotation Class And @isTest Method
  2. Prepared your test data. sometimes. Maybe you can @testSetup. if you have many methods use similar data.

Problem

  1. Mixture the Unit Test And Integration Test.If the related method failed. And your Unit Test will be failed.
  2. There are many unrelated methods runs. And will hit the governor limits easily.
  3. Unit Test running time will increase.

These Unit Test Not really Unit Test. Highly recommend Unit Test should more isolate and don't reply on other class or the database data.

Dependency Injection

When the Business Running. And the run real logic code. When the environment is running Test context. Should invoke our pre-define method or mock object.

sfab_FabricatedSObject fabricatedAccount = new sfab_FabricatedSObject(Account.class);
fabricatedAccount.setField(Account.Id, 'Id-1');
fabricatedAccount.setField(Account.Name, 'Bubba');
fabricatedAccount.setField(Account.LastModifiedDate, Date.newInstance(2017, 1, 1));

Account acct = (Account)fabricatedAccount.toSObject();

// Account:{LastModifiedDate=2017-01-01 00:00:00, Id=Id-1}
System.debug(acct);
// (Opportunity:{Id=OppId-1}, Opportunity:{Id=OppId-2})
System.debug(acct.Opportunities);

sfab_FabricatedSObject fabricatedOpportunity = new sfab_FabricatedSObject(Opportunity.class);
fabricatedOpportunity.setField(Opportunity.Id, 'OppId-3');
fabricatedOpportunity.setField(Opportunity.Name, 'TestOpp');
fabricatedOpportunity.setParent('Account', fabricatedAccount);
fabricatedOpportunity.setField(Opportunity.LastModifiedDate, Date.newInstance(2017, 1, 1));

Opportunity opp = (Opportunity)fabricatedOpportunity.toSObject();
system.debug(opp.Account);
system.debug(opp);
system.debug(opp.Account.Name);
  • Advantage
    1. Implements the isolates Unit Test.
    2. Prepare Test Data Fast and more efficient for UT
    3. Reduce the database interaction and Unit Test Time

Suggested Practice

  1. The test method function names and class name should be on behalf of the method.
  2. A function should be smaller. if there are too many lines. You should consider refactor.
  3. All test class methods should be marked private and the class itself should be marked private.
  4. The test class needs to be self-contained - we shouldn't create any utility test classes for setting up data.
  5. The test methods should utilize the fflib framework and sfab frameworks to set up objects instead of raw JSON in the test methods.
  6. The test classes should avoid any DML and SOQL operations.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子赦拘,更是在濱河造成了極大的恐慌捎谨,老刑警劉巖蹭沛,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件根欧,死亡現(xiàn)場離奇詭異酥泛,居然都是意外死亡灰瞻,警方通過查閱死者的電腦和手機燎竖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人托猩,你說我怎么就攤上這事厅各〖赐耄” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵烙样,是天一觀的道長蕊肥。 經(jīng)常有香客問我谒获,道長壁却,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任赔硫,我火速辦了婚禮盐肃,結(jié)果婚禮上爪膊,老公的妹妹穿的比我還像新娘砸王。我一直安慰自己,他們只是感情好谦铃,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瘪菌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪师妙。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天曾掂,我揣著相機與錄音惫谤,去河邊找鬼。 笑死若专,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的调衰。 我是一名探鬼主播自阱,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼沛豌!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起加派,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤芍锦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后娄琉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡票腰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年匈棘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡鹃愤,死狀恐怖完域,靈堂內(nèi)的尸體忽然破棺而出软吐,到底是詐尸還是另有隱情吟税,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布肖抱,位于F島的核電站异旧,受9級特大地震影響意述,放射性物質(zhì)發(fā)生泄漏吮蛹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一术荤、第九天 我趴在偏房一處隱蔽的房頂上張望每篷。 院中可真熱鬧瓣戚,春花似錦雳攘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽无畔。三九已至吠冤,卻和暖如春浑彰,著一層夾襖步出監(jiān)牢的瞬間拯辙,已是汗流浹背颜价。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工诉濒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人未荒。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像寨腔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子迫卢,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容