Salesforce中使用Apex Test Class(测试类)测试Extension类型实例

场景说明:最近在写Extension类型测试类时,对于通过Url中的参数去访问某个特定记录页面的测试类,由于缺乏经验无法达到比较理想的覆盖率(>75%),在参考了相关资料后,特意对Extension类的测试类,做以下总结。

1、Apex Extension Class Sample:

/**********************************************************************
*Name:供应商个人中心扩展
*Description:供应商可以在广告商中心查看自己的个人信息,并提交修改信息申请
======================================================
History                                                            
-------                                                            
VERSION  AUTHOR         DATE            DETAIL                                
1.0      Wilson Xu      2017-09-15      Created
***********************************************************************/ 
public class DealerCenterExtension {
    public List conList {get;set;}
    public Contact cont {get;set;}
    public String accountId;
    public String description {get;set;}
    public String userId;

    public DealerCenterExtension(ApexPages.StandardController controller) {
    	userId = System.Label.UserId;
    	accountId = ApexPages.currentPage().getParameters().get('id');
    	// 查找该供应商的第一个联系人
        conList = [SELECT LastName, Phone FROM Contact WHERE AccountId = :accountId];
        if(conList.size() > 0) {
            cont = conList[0];
        }
    }

    // 提交修改申请
    public void modifyApplication() {
	if(description.length() > 0) {
            Account acc = [SELECT Name FROM Account WHERE Id = :accountId];
            try{
            	// 新建一个任务
		Task task = new Task();
		task.Subject = acc.Name + '信息修改申请';
		task.whatId = accountId;// 相关项目,这里是供应商Id
		task.ownerId = userId;// 被分配人,这里是UserId
		task.Description = description;
		task.status = 'open';
		task.priority = 'High';
		task.IsReminderSet = true;// 设置提醒
		task.ReminderDateTime = System.now();
		insert task;
		ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM,'供应商信息修改申请提交成功!'));
            }catch(DmlException e) {
            	System.debug('Task Create Error: ' + e.getMessage());
            	ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING,'供应商信息修改申请提交失败,请联系系统管理员!'));
            }
        }else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING,'请在修改备注里面按照实例填写修改详情!'));
        }
    }

}

2、Test Class Sample:

@isTest
private class DealerCenterExtensionTest {
    static testMethod void test() {
        // 供应商
    	Account a1 = new Account();
        a1.Name = '测试广告商';
        a1.Approval_Status__c = '审核通过';
        a1.Advertiser_Email__c = '[email protected]';
        a1.SAP_Ads_Number__c = '111111';
        insert a1;

        // 联系人
        Contact c1 = new Contact();
        c1.LastName = 'test';
        c1.Phone = '18812312211';
        c1.AccountId = a1.Id;
        insert c1;
        // 为页面传入参数,因此构造方法中可以通过Id查询记录
        PageReference prf = Page.DealerCenter;
        prf.getParameters().put('id',a1.Id); 
        Test.setCurrentPage(prf);
        //实例化该Extension类
        DealerCenterExtension ac = new DealerCenterExtension(new ApexPages.StandardController(a1));
        ac.description = '';
        ac.modifyApplication();
        ac.description = 'test';
        ac.modifyApplication();

    }
}

3、详细解释:

对于依赖parameter的custom page,在写测试类时,需要预先设置参数

ApexPages.currentPage().getParameters().put('id', accountId);

对于Extension类实例化时,注意传参,参数为标准控制器的实例化对象;

4、其他经验之谈:

通过代码创建Task时,需要注意必填项及其含义,其中被分配人为ownerId, 相关项目为whatId。另外,如果需要设置提醒的话,IsReminderSet和ReminderDateTime都填写后才会生效。

5、扩展:Unit testing redirect in controller to external website [closed]
getUrl()方法对于external url来说,是website的完全路径;对于internal url来说,是相对路径;
Salesforce中使用Apex Test Class(测试类)测试Extension类型实例_第1张图片
相关PD2考题:
Salesforce中使用Apex Test Class(测试类)测试Extension类型实例_第2张图片

 

你可能感兴趣的:(Sales,Cloud,Salesforce,Experience,Extension,Test,Class,Task)