測(cè)試是Apex或任何其他應(yīng)用程序開發(fā)的集成部分。 在Apex中,我們有單獨(dú)的測(cè)試類來開發(fā)所有的單元測(cè)試。
讓我們?yōu)槲覀冎熬帉懙囊粋€(gè)代碼編寫一個(gè)測(cè)試類。 我們將編寫測(cè)試類來覆蓋我們的Trigger和Helper類代碼。 下面是需要覆蓋的觸發(fā)器和幫助類。
//Trigger with Helper Class trigger Customer_After_Insert on APEX_Customer__c (after update) { CustomerTriggerHelper.createInvoiceRecords(Trigger.new, trigger.oldMap);//Trigger calls the helper class and does not have any code in Trigger } //Helper Class: public class CustomerTriggerHelper { public static void createInvoiceRecords (List<apex_customer__c> customerList, Map<id, apex_customer__c> oldMapCustomer) { List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>(); for (APEX_Customer__c objCustomer: customerList) { if (objCustomer.APEX_Customer_Status__c == 'Active' && oldMapCustomer.get(objCustomer.id).APEX_Customer_Status__c == 'Inactive') {//condition to check the old value and new value APEX_Invoice__c objInvoice = new APEX_Invoice__c(); objInvoice.APEX_Status__c = 'Pending'; objInvoice.APEX_Customer__c = objCustomer.id; InvoiceList.add(objInvoice); } } insert InvoiceList;//DML to insert the Invoice List in SFDC } }
數(shù)據(jù)創(chuàng)建
我們需要在測(cè)試類本身創(chuàng)建測(cè)試類的數(shù)據(jù)。默認(rèn)情況下,測(cè)試類不能訪問組織數(shù)據(jù),但是如果您設(shè)置@isTest(seeAllData = true),那么它將有權(quán)訪問組織的數(shù)據(jù)。
@isTest注釋
通過使用此注釋,您聲明這是一個(gè)測(cè)試類,它不會(huì)被計(jì)入組織的總代碼限制。
TestMethod關(guān)鍵字
單元測(cè)試方法是不帶參數(shù),不向數(shù)據(jù)庫(kù)提交數(shù)據(jù),不發(fā)送電子郵件,并在方法定義中使用testMethod關(guān)鍵字或isTest注釋聲明的方法。此外,測(cè)試方法必須在測(cè)試類中定義,即用isTest注釋的類。在我們的例子中,我們將看到,'myUnitTest'是我們的測(cè)試方法。
Test.startTest()和Test.stopTest()
這些是可用于測(cè)試類的標(biāo)準(zhǔn)測(cè)試方法。這些方法包含我們將模擬我們的測(cè)試的事件或動(dòng)作。就像在這個(gè)例子中,我們將測(cè)試我們的觸發(fā)器和幫助類來模擬火災(zāi)觸發(fā)器,通過更新記錄,我們已經(jīng)做了開始和停止塊。這也為在開始和停止塊中的代碼提供單獨(dú)的調(diào)節(jié)器限制。
System.assert()
此方法用實(shí)際檢查所需的輸出。在這種情況下,我們期望插入一個(gè)發(fā)票記錄,所以我們添加了assert來檢查。
例如:
/** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * * See the Apex Language Reference for more information about Testing and Code Coverage. */ @isTest private class CustomerTriggerTestClass { static testMethod void myUnitTest() { //Create Data for Customer Objet APEX_Customer__c objCust = new APEX_Customer__c(); objCust.Name = 'Test Customer'; objCust.APEX_Customer_Status__c = 'Inactive'; insert objCust; //Now, our trigger will fire on After update event so update the Records Test.startTest();//Starts the scope of test objCust.APEX_Customer_Status__c = 'Active'; update objCust; Test.stopTest();//Ends the scope of test //Now check if it is giving desired results using system.assert Statement.New invoice should be created List<apex_invoice__c> invList = [SELECT Id, APEX_Customer__c FROM APEX_Invoice__c WHERE APEX_Customer__c = :objCust.id]; system.assertEquals(1,invList.size());//Check if one record is created in Invoivce sObject } }
按照下面的步驟來運(yùn)行測(cè)試類:
第1步:轉(zhuǎn)到Apex classes =>單擊類名稱'CustomerTriggerTestClass'
第2步:如圖所示,點(diǎn)擊運(yùn)行測(cè)試按鈕:
第3步:檢查狀態(tài)
第4步:現(xiàn)在檢查我們已經(jīng)寫入測(cè)試的類和觸發(fā)器
類:
觸發(fā):
更多建議: