+ Start a Discussion

Code Coverage for Apex Class?

I wrote the following class to call from my triggers and ensure they don't run recursively. This is the code:

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
     return true;
        return run;

Do I need to write a test class for this class in order to push it to our instance? If so, can someone point me in the right direction? Thanks.
Yes.every class needs it that is to be moved to Prod. However if you are using test class for the triggers and have test class for triggers, then it would automatically provide code coverage for this class too if your test class runs with both conditions - run variable as true and false.
Perfect. That helps so much. Thank you!
Actually one more question: My trigger deals with opportunities. The test class for it just creates an opportunity and manipulates it to test that the trigger is working right. How do I access the run variable using apex code to make sure it's tested?
Amit Chaudhary 8Amit Chaudhary 8
In which Trigger you are using above class. If you are using same class in contact or Opportunity then simply just create the object of contact or opportunity and perform one insert and 2nd update contact. That code will cover
This is my test: I'm not exactly sure what all I need to do twice...

@isTest (seeAllData=true)
private class testEditSchedule{

static testMethod void testUpdateSchedule() {
List<Account> aList = new List<Account>();    
Account a = new Account(
      Name = 'Test Account'
    insert aList;

List<Opportunity> oList = new List<Opportunity>();
Opportunity o = new Opportunity(
      Name = 'Test Opportunity', 
      StageName = 'Discovery', 
      CloseDate = Date.today(),
      Contract_Start_Date__c = Date.today(), 
      AccountId = a.Id
    insert oList;
        //Create Product
        Product2 prod = new Product2();
        prod.IsActive = true;
        prod.Name = 'Onboarding';
        insert prod;
        //Create PriceBook
        Pricebook2 pb = new Pricebook2();
        pb.Name = 'Test';
        pb.IsActive = true;
        insert pb;
        List<Pricebook2> PbList = [Select Id, Name, IsActive From Pricebook2 where IsStandard = true LIMIT 1];
    //Pricebook2 standardpb = [Select Id, Name, IsActive From Pricebook2 where IsStandard = true LIMIT 1];
        PricebookEntry standardpbe = new PricebookEntry();
        standardpbe.Pricebook2Id = PbList[0].Id;
        standardpbe.Product2Id = prod.Id;
        standardpbe.UnitPrice = 10000;
        standardpbe.IsActive = true;
        standardpbe.UseStandardPrice = false;
        insert standardpbe;
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = pb.Id;
        pbe.Product2Id = prod.Id;
        pbe.UnitPrice = 10000;
        pbe.IsActive = true;
        pbe.UseStandardPrice = false;
        insert pbe;
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); 
List<OpportunityLineItem> oliAnnuallyList = new List<OpportunityLineItem>(); 
List<OpportunityLineItem> oliQuarterlyList = new List<OpportunityLineItem>(); 
List<OpportunityLineItem> oliMonthlyList = new List<OpportunityLineItem>(); 
OpportunityLineItem oliAnnually = new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Payment_Terms__c ='Annually',
        Duration__c = 12
    OpportunityLineItem oliMonthly= new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Payment_Terms__c ='Monthly',
        Duration__c = 1
    OpportunityLineItem oliQuarterly= new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Payment_Terms__c ='Quarterly',
        Duration__c = 3
    OpportunityLineItem oli= new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Payment_Terms__c = 'Up Front',
        Duration__c = 12
    insert oliList;
    insert oliAnnuallyList;
    insert oliQuarterlyList;
    insert oliMonthlyList;
    //Your trigger should have OpportunityLineItemSchedule logic then following asert will pass else it will fail
    List<OpportunityLineItem> lstOptyLi =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oli.id];
    System.assertEquals(True, lstOptyLi[0].HasRevenueSchedule);
    List<OpportunityLineItem> lstOptyLi_2 =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oliAnnually.id];
    System.assertEquals(True, lstOptyLi_2[0].HasRevenueSchedule);
    List<OpportunityLineItem> lstOptyLi_3 =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oliQuarterly.id];
    System.assertEquals(True, lstOptyLi_3[0].HasRevenueSchedule);
    List<OpportunityLineItem> lstOptyLi_4 =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oliMonthly.id];
    System.assertEquals(True, lstOptyLi_4[0].HasRevenueSchedule);