课程表

Apex课程

工具箱
速查手册

Apex - 测试

当前位置:免费教程 » 程序设计 » Apex

测试是Apex或任何其他应用程序开发的集成部分。 在Apex中,我们有单独的测试类来开发所有的单元测试。


测试类

在SFDC中,代码必须具有75%的代码覆盖率才能部署到生产。 此代码覆盖率由测试类执行。 测试类是测试其他Apex类的功能的代码片段。

让我们为我们之前编写的一个代码编写一个测试类。 我们将编写测试类来覆盖我们的Trigger和Helper类代码。 下面是需要覆盖的触发器和帮助类。

  1. //Trigger with Helper Class
  2. trigger Customer_After_Insert on APEX_Customer__c (after update) {
  3. CustomerTriggerHelper.createInvoiceRecords(Trigger.new, trigger.oldMap);//Trigger calls the helper class and does not have any code in Trigger
  4. }
  5. //Helper Class:
  6. public class CustomerTriggerHelper {
  7. public static void createInvoiceRecords (List<apex_customer__c> customerList, Map<id, apex_customer__c> oldMapCustomer) {
  8. List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>();
  9. for (APEX_Customer__c objCustomer: customerList) {
  10. 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
  11. APEX_Invoice__c objInvoice = new APEX_Invoice__c();
  12. objInvoice.APEX_Status__c = 'Pending';
  13. objInvoice.APEX_Customer__c = objCustomer.id;
  14. InvoiceList.add(objInvoice);
  15. }
  16. }
  17. insert InvoiceList;//DML to insert the Invoice List in SFDC
  18. }
  19. }

创建测试类

数据创建

我们需要在测试类本身创建测试类的数据。默认情况下,测试类不能访问组织数据,但是如果您设置@isTest(seeAllData = true),那么它将有权访问组织的数据。


@isTest注释

通过使用此注释,您声明这是一个测试类,它不会被计入组织的总代码限制。


TestMethod关键字

单元测试方法是不带参数,不向数据库提交数据,不发送电子邮件,并在方法定义中使用testMethod关键字或isTest注释声明的方法。此外,测试方法必须在测试类中定义,即用isTest注释的类。

在我们的例子中,我们将看到,'myUnitTest'是我们的测试方法。


Test.startTest()Test.stopTest()

这些是可用于测试类的标准测试方法。这些方法包含我们将模拟我们的测试的事件或动作。就像在这个例子中,我们将测试我们的触发器和帮助类来模拟火灾触发器,通过更新记录,我们已经做了开始和停止块。这也为在开始和停止块中的代码提供单独的调节器限制。


System.assert()

此方法用实际检查所需的输出。在这种情况下,我们期望插入一个发票记录,所以我们添加了assert来检查。


例如:

  1. /**
  2. * This class contains unit tests for validating the behavior of Apex classes
  3. * and triggers.
  4. *
  5. * Unit tests are class methods that verify whether a particular piece
  6. * of code is working properly. Unit test methods take no arguments,
  7. * commit no data to the database, and are flagged with the testMethod
  8. * keyword in the method definition.
  9. *
  10. * All test methods in an organization are executed whenever Apex code is deployed
  11. * to a production organization to confirm correctness, ensure code
  12. * coverage, and prevent regressions. All Apex classes are
  13. * required to have at least 75% code coverage in order to be deployed
  14. * to a production organization. In addition, all triggers must have some code coverage.
  15. *
  16. * The @isTest class annotation indicates this class only contains test
  17. * methods. Classes defined with the @isTest annotation do not count against
  18. * the organization size limit for all Apex scripts.
  19. *
  20. * See the Apex Language Reference for more information about Testing and Code Coverage.
  21. */
  22. @isTest
  23. private class CustomerTriggerTestClass {
  24. static testMethod void myUnitTest() {
  25. //Create Data for Customer Objet
  26. APEX_Customer__c objCust = new APEX_Customer__c();
  27. objCust.Name = 'Test Customer';
  28. objCust.APEX_Customer_Status__c = 'Inactive';
  29. insert objCust;
  30. //Now, our trigger will fire on After update event so update the Records
  31. Test.startTest();//Starts the scope of test
  32. objCust.APEX_Customer_Status__c = 'Active';
  33. update objCust;
  34. Test.stopTest();//Ends the scope of test
  35. //Now check if it is giving desired results using system.assert Statement.New invoice should be created
  36. List<apex_invoice__c> invList = [SELECT Id, APEX_Customer__c FROM APEX_Invoice__c WHERE APEX_Customer__c = :objCust.id];
  37. system.assertEquals(1,invList.size());//Check if one record is created in Invoivce sObject
  38. }
  39. }

运行测试类

按照下面的步骤来运行测试类:


第1步:转到Apex classes =>单击类名称'CustomerTriggerTestClass'


第2步:如图所示,点击运行测试按钮:


CustomerTriggerTestClass


第3步:检查状态


检查状态


第4步:现在检查我们已经写入测试的类和触发器


类:


Class


触发:


Trigger
我们的测试成功并完成。
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号