如何获得部署Apex Class所需的代码覆盖率

时间:2017-09-30 02:14:55

标签: salesforce apex

请帮助!!! 我有这个Apex控制器类,我试图从Salesforce Sandbox部署到Production,但我没有得到所需的代码覆盖率!

该类占用3个对象,其中一个是搁浅的,2个是自定义的,并为这三个对象构建层次树视图。

// Apex Class

公共类TeeView {

        /* Wrapper class to contain the nodes and their children */
 public class cNodes
{
    Public StandardObject gparent {get;set;}
    public List<CustomObject1__c> parent {get; set;}
    Public List<CustomObject2__c> child {get;set;}

    public cNodes(StandardObject   gp, List<CustomObject1__c> p, List<CustomObject2__c> c)
    {
    parent = p;
    gparent = gp;
    child = c;
    }
}

        /* end of Wrapper class */ 

Public List<cNodes> hierarchy;

Public List<cNodes> getmainnodes()
{
    hierarchy = new List<cNodes>();
    List<StandardObject> tempparent = [Select Id,Name  , End_Date__c, Owner.Name Account.Name from Contract ];
    for (Integer i =0; i< tempparent.size() ; i++)
        {
        List<CustomObject1__c> tempchildren = [Select Id,Name, Owner.Name , (select Id,Name, Owner.Name from CustomObject2__r)  from CustomObject1__c where    Related_Field__c = :tempparent[i].Id];
        List<CustomObject2__c> tempchild = [Select Id,Name Owner.Name from CustomObject2__c where    Related_Field__c= :tempparent[i].Id];

        hierarchy.add(new cNodes(tempparent[i],tempchildren, tempchild));
        }   
    return hierarchy;
}  

}

//测试类 @isTest 公共类treeviewTest {

static testMethod void test1()
{
    test.startTest();


    Account acc = new Account(Name = 'Unit test account');
    insert acc;

    StandardObject c = new StandardObject(
        Name = 'test',
        AccountId = acc.Id,
        Status = 'Draft',
        StartDate = System.today());
    try
    {
        insert c;
    }
    catch (Exception ex)
    {
        ApexPages.addMessages(ex);
    }

    List<StandardObject> standard = [select Id, Name from StandardObject where Name = 'test'];
    system.assertequals(standard.size(), 1);

    CustomObject1__c s = new CustomObject1__c(
        Related_StandardObjectField__c = c.Id,
        Name = 'test'
    );
    try
    {
        insert s;
    }
    catch (Exception ex)
    {
        ApexPages.addMessages(ex);
    }

    List<CustomObject1__c> cus1 = [select Id, Name from CustomObject1__c where Name = 'test'];
    system.assertequals(cus1.size(), 1);

    insert new CustomObject2__c(Related_StandardObjectField__c = c.Id, Description__c = 'test');
    List<CustomObject2__c> cus2 = [select Id, Name from CustomObject2__c where Description__c = 'test'];
    system.assertequals(cus2.size(), 1);

    insert new CustomObject2__c(Related_CustomObject1Field__c = s.Id, Description__c = 'test');
    List<Mods__c> cus3 = [select Id, Name from Mods__c where Description__c = 'test'];
    system.assertequals(cus3.size(), 1);

    treeView view = new treeView();

    view.getmainnodes();


    test.stopTest();
}

}

2 个答案:

答案 0 :(得分:0)

噢,伙计,我们在哪里开始......看起来你随意复制粘贴一些代码试图让测试工作?您的代码适用于Contract, CustomObject1__c, CustomObject2__c。您的单元测试会插入Contract, Subaward__c, Mods__c。这是否试图匿名代码?那个List<StandardObject>的东西是什么,它甚至不应该编译。

我认为您需要具有更多经验的开发人员的帮助,您已经拼凑了一些东西,但它并没有遵循Salesforce的代码最佳实践...

关于主要课程

智能使用subqueries意味着

  • 你浪费的SOQL更少(盲目选择所有记录而不使用任何过滤器,但无论如何),
  • 不要遇到与过多的SOQL语句相关的错误&#34; (限制为100因此,因为您在循环中查询 - 您的代码将在您拥有大约50个合同时崩溃并刻录)
  • 导致紧凑的代码占用更少的线(更容易维护和覆盖测试)。

我选择类似的东西,查询合同&amp;首先是一个相关的列表,然后是另一个查询中的父母+孩子。使用Map链接它们进行显示。

List<Contract> levelOneAndTwo = [SELECT Id, Name, End_Date__c, Owner.Name, Account.Name,
    (SELECT Id,Name, Owner.Name FROM Subawards__r)
FROM Contract];

Map<Id, SubAward__c> levelTwoAndThree = new Map<Id, Subaward__c>([SELECT Id,
    (SELECT Id,Name Owner.Name FROM Mods__r)
    FROM Subaward__c WHERE Contract__c = :levelOneAndTwo];
);

这只会浪费2个查询但会获取所有合同及其相关数据。然后,您可以遍历构建包装器对象的结果,或者直接将其提供给Visualforce进行显示。

关于单元测试

你没有获得保险,因为你的传播太薄了。一个单元测试方法仅插入合同(因此您的主要查询将触及某些内容,为您提供一些覆盖)。然后另一种方法(在完全不同的上下文中)插入一些子对象 - 但它们并不重要。在另一种方法中,有0个合约,所以它甚至不会进入你的主循环。把它合并成一个?

Account acc = new Account(Name = 'Unit test account');
insert acc;

Contract c = new Contract(
    Name = 'test contract',
    AccountId = acc.Id,
    Status = 'Draft',
    StartDate = System.today()
);
insert c;

Subaward__c s = new Subaward(
    Contract__c = c.Id,
    Name = 'test'
);
insert s;

insert new Mod__c(Subaward__c = c.Id, Description__c = 'test');

现在您已经设置了很好的树层次结构,从Account一直向下。测试中的查询应该会显示一些数据。

答案 1 :(得分:0)

通过将这些行添加到测试类,我终于能够获得上述类的%100代码覆盖率:

     treeView view = new treeView();
                view.getmainnodes();
                    system.assert(view.hierarchy.size() > 0);