尝试测试自定义潜在客户转化类别,但获得0%的代码覆盖率

时间:2019-09-02 10:31:35

标签: unit-testing salesforce apex test-class

我正在测试以下类别,即转换潜在客户,并且我正在为此编写测试类别,但是该测试类别甚至覆盖不到1%的代码。

global class LeadConversion
{
    Public Id leadId;
    public LeadConversion(ApexPages.StandardController stdController)
    {
        leadId = ApexPages.currentPage().getParameters().get('id');
    }
    public PageReference autoConvert()
    {
        Database.LeadConvert convertLead = new database.LeadConvert();
        convertLead.setLeadId(leadId);
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        convertLead.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult convertLeadResult = Database.convertLead(convertLead);

        //TO get Account ID from Lead Conversion
        Id accountId = convertLeadResult.getAccountId();
        PageReference Page = new PageReference('https://----Here My Org URl---/'+accountId);
        Page.setRedirect(true);
        return Page;
    }
}

测试类

@isTest
public class TestLeadConversion {
    static testMethod void LeadConvertedTest()
    {
        Lead lead1 = new Lead(LastName='Test',FirstName='Tester',Status='Known',Company='test');
        insert lead1;

        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(lead1.id);
        lc.setConvertedStatus('Closed - Converted');

        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess()); 
    }
}

1 个答案:

答案 0 :(得分:1)

使用下面的代码,看看是否可行。并且请记住将Your_Page_Name替换为您的页面名称。

@isTest
public class TestLeadConversion {
    static testMethod void LeadConvertedTest()
    {
        //Create New Lead
        Lead lead1 = new Lead(LastName='Test',FirstName='Tester',Status='Known',Company='test');
        insert lead1;

        PageReference pageRef = Page.Your_Page_Name;
        //TO get Account ID from Lead Conversion
        pageRef.getParameters().put('id', lead1.id);

        Test.setCurrentPage(pageRef);
        ApexPages.StandardController sc = new ApexPages.standardController(lead1);
        LeadConversion classController = new LeadConversion(sc);
        System.assertNotEquals(null, classController.autoConvert());
    }
}