【问题标题】:Apex Class zero code coverageApex Class 零代码覆盖率
【发布时间】:2015-02-09 02:46:27
【问题描述】:

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

我没有收到任何错误,但我没有收到任何代码覆盖率。任何帮助都会大有帮助。

提前致谢

Apex 类

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);
    }
}

【问题讨论】:

    标签: salesforce apex


    【解决方案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);) 时,您将绕过该工作。

    【讨论】:

      猜你喜欢
      • 2016-01-12
      • 2018-04-16
      • 1970-01-01
      • 2018-02-17
      • 2016-11-13
      • 2012-06-30
      • 2019-08-12
      • 2018-01-11
      • 1970-01-01
      相关资源
      最近更新 更多