Apex Class零代码覆盖率

时间:2014-12-10 19:08:25

标签: salesforce apex

我是Apex的新手......进入它的一天,对我的抱怨感到抱歉。我要做的是返回我的帐户列表,然后查看帐户的合同日期(Contract_Expiration__c)。根据该日期,它应更新客户字段( update_active_status_text__c),null,Active或Void。

我没有收到任何错误,但我没有得到任何代码覆盖率。任何帮助都会有很长的路要走。

提前致谢

Apex Class

public class update_active_status {
    public static VOID update_active_statustest(){
        list<Account> myaccount = [SELECT Id, Contract_Expiration__c, update_active_status_text__c FROM Account WHERE CMS_Customer_Type__c = 'Enterprise' or CMS_Customer_Type__c = 'cloud'];
            for(Account a: myaccount){
           if (a.Contract_Expiration__c == null ){ 
                   a.update_active_status_text__c = null;
                   update a;
               } else if (a.Contract_Expiration__c >= Date.today().addDays(-60)) {
                a.update_active_status_text__c = 'Active';
                   update a;
               } else if (a.Contract_Expiration__c < Date.today().addDays(-60)) {
                a.update_active_status_text__c = 'Void';
                   update a;
               } else {
                a.update_active_status_text__c = 'Void';
                  update a;
               }    
        }
     }
}

测试类

@isTest
public class testupdate_active_status {
    static testMethod void myupdate_active_statusTest() {
        Account acc = new Account(Name = 'Test Account');
        insert(acc);

        Date d = Date.today();
        acc.Contract_Expiration__c = d;
        update(acc);

        acc.update_active_status_text__c = 'Active';
        update(acc);

        acc = [Select update_active_status_text__c From Account Where Id = : acc.Id];
        System.assertEquals('Active', acc.update_active_status_text__c);

    }

    static testMethod void setToNull() {
        Account acc = new Account(Name = 'Test Account');
        insert(acc);

        Date d = Date.today();
        acc.Contract_Expiration__c = d;
        update(acc);

        acc.Contract_Expiration__c = null;
        update(acc);

        acc = [Select update_active_status_text__c From Account Where Id = : acc.Id];
        System.assertEquals(null, acc.update_active_status_text__c);

    }

    static testMethod void createWithDate() {
        Date d = Date.today().addDays(-70);
        Account acc = new Account(Name = 'Test Account', Contract_Expiration__c = d);
        insert(acc);

        acc.update_active_status_text__c = 'Void';
        update(acc);

        acc = [Select update_active_status_text__c From Account Where Id = : acc.Id];
        System.assertEquals('Void', acc.update_active_status_text__c);
    }
}

1 个答案:

答案 0 :(得分:1)

您的测试实际上并未使用您编写的类中的任何方法。也许:

@isTest
public class testupdate_active_status {
    static testMethod void myupdate_active_statusTest() {
        Account acc = new Account(Name = 'Test Account');
        insert(acc);

        Date d = Date.today();
        acc.Contract_Expiration__c = d;
        update(acc);

        testupdate_active_status.update_active_statustest();   // Call your new method!

        acc = [Select update_active_status_text__c From Account Where Id = : acc.Id];
        System.assertEquals('Active', acc.update_active_status_text__c);

    }

    static testMethod void setToNull() {
        Account acc = new Account(Name = 'Test Account');
        insert(acc);

        Date d = Date.today();
        acc.Contract_Expiration__c = d;
        update(acc);

        testupdate_active_status.update_active_statustest();   // Call your new method!

        acc = [Select update_active_status_text__c From Account Where Id = : acc.Id];
        System.assertEquals(null, acc.update_active_status_text__c);

    }

    static testMethod void createWithDate() {
        Date d = Date.today().addDays(-70);
        Account acc = new Account(Name = 'Test Account', Contract_Expiration__c = d);
        insert(acc);

        testupdate_active_status.update_active_statustest();   // Call your new method!

        acc = [Select update_active_status_text__c From Account Where Id = : acc.Id];
        System.assertEquals('Void', acc.update_active_status_text__c);
    }
}

您的新班级的工作是更新update_active_status_text__c ...当您自己手动/显式更新该字段时,您将绕过该作业(acc.update_active_status_text__c = 'Active'; update(acc);)。