function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shawn Reichner 29Shawn Reichner 29 

Test Class only covering 70 percent - Please help

Hello Awesome Devs!!!

I have the following Scheduled Class, and test class, and I am only able to get the code coverage on this class to 70 percent.  Looks like the issue is in my IF , ELSE IF statements, as the Null check always tests out, but if I am stating that the field is not null/blank then the code aftere that doe snot get covered.  

Can anyone help me figure out what is going on here and help to get this above the acceptable coverage limit?

Appreciate any help you can provide,

Shawn

Class - 
 
global class CreateUpgradeDowngradeOpportunities Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        CreateOpportunities();
    }
      
    public void CreateOpportunities()
    {
        //Variable & List Declarations
        
        Date Today = Date.Today();
        Date d1 = Today.addDays(-1);
		List<Opportunity> lstOpp = new List<Opportunity>();
		Opportunity objOpp;
		Id idRecTypeDowngrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Downgrade').getRecordTypeId();
		Id idRecTypeUpgrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Upgrade').getRecordTypeId();
        User FH = [Select ID FROM User WHERE FirstName = 'Firehost' LIMIT 1];
		Map<Id, Account> mapAcc = new Map<Id, Account>();
		Id idAcc;
		String strStatus;
		Decimal amt;
		Set<Id> setIdAcc = new Set<Id>();
		List<AggregateResult> lstAR = new List<AggregateResult>();
		
		// collect sum by account
		for(AggregateResult objAR : [SELECT Zuora__Account__c , Zuora__Status__c, SUM(Zuora__MRR__C) 
									FROM Zuora__Subscription__c 
									WHERE Zuora__Status__c IN ('Cancelled', 'Active')
										AND Zuora__Account__c != NULL
										AND (Zuora__TermStartDate__c = YESTERDAY OR Zuora__TermEndDate__c = YESTERDAY)
                                     // Add additional Criteria here..
									GROUP BY ROLLUP(Zuora__Account__c, Zuora__Status__c)])
		{
			lstAR.add(objAR);
			setIdAcc.add((Id)objAR.get('Zuora__Account__c'));
		} // End of Aggregate For Loop
		
		// collect account infos
		if(!setIdAcc.isEmpty())
		{
			mapAcc = new Map<Id, Account>([SELECT Id, Name, OwnerId, Service_Lead__c FROM Account WHERE Id IN: setIdAcc]);   
		}
		
		// create opps
		for(AggregateResult objAR : lstAR)
		{
			idAcc = (Id)objAR.get('Zuora__Account__c');
			strStatus = (String)objAR.get('Zuora__Status__c');
			amt = (Decimal)objAR.get('expr0');
            
			if(strStatus == 'Cancelled' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeDowngrade;
				objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Downgrade';
				objOpp.Amount = amt * -1;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Cancelled';    
				lstOpp.add(objOpp);    
			}
            else if(strStatus == 'Cancelled' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeDowngrade;
				objOpp.OwnerId = FH.Id;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Downgrade';
				objOpp.Amount = amt * -1;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Cancelled';    
				lstOpp.add(objOpp);    
			}
			else if(strStatus == 'Active' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeUpgrade;
				objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Existing Business';
				objOpp.Amount = amt;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Closed Won';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
				lstOpp.add(objOpp);
			}
            else if(strStatus == 'Active' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeUpgrade;
				objOpp.OwnerId = FH.Id;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Existing Business';
				objOpp.Amount = amt;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Closed Won';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
				lstOpp.add(objOpp);
			}
		} // END of FOR Loop

		if(!lstOpp.isEmpty())
		{
			insert lstOpp;
		}
    } // End of CreateOpportunities Method 
} // End of Class

Test Class -
 
@isTest(SeeAllData = True)
public class CreateUpgradeDowngradeOpportunitiesTest {

    List<Zuora__Subscription__c> subs = new List<Zuora__Subscription__c>();
    
    
    private static testMethod void tm1(){
        
    	Date Today = Date.Today();
    	Date d1 = Today.addDays(-1);
        User u = [SELECT ID FROM USER WHERE FirstName = 'FireHost' LIMIT 1];
        
        Account a = New Account();
        a.Name = 'Test Account';
        a.Status__c = 'Active';
        insert a;
        
        Account a2 = New Account();
        a2.Name = 'Test Account 2';
        a.Status__c = 'Active';
        a.Service_Lead__c = u.Id;
        insert a2;
        
        Zuora__Subscription__c s = new Zuora__Subscription__c();
        s.Zuora__Account__c = a.Id;
        s.Zuora__Status__c = 'Active';
        s.Zuora__MRR__c = 100;
        s.Zuora__TermStartDate__c = d1;
        insert s;
        
        Zuora__Subscription__c s2 = new Zuora__Subscription__c();
        s2.Zuora__Account__c = a.Id;
        s2.Zuora__Status__c = 'Cancelled';
        s2.Zuora__MRR__c = 100;
        s2.Zuora__TermStartDate__c = d1;
        insert s2;
        
        Zuora__Subscription__c s3 = new Zuora__Subscription__c();
        s3.Zuora__Account__c = a2.Id;
        s3.Zuora__Status__c = 'Active';
        s3.Zuora__MRR__c = 100;
        s3.Zuora__TermStartDate__c = d1;
        insert s3;
        
        Zuora__Subscription__c s4 = new Zuora__Subscription__c();
        s4.Zuora__Account__c = a2.Id;
        s4.Zuora__Status__c = 'Cancelled';
        s4.Zuora__MRR__c = 100;
        s4.Zuora__TermStartDate__c = d1;
        insert s4;


        String CRON_EXP = '0 0 0 15 3 ? *';
        

		String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new CreateUpgradeDowngradeOpportunities());
		CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
		System.assertEquals(CRON_EXP, ct.CronExpression);
		System.assertEquals(0, ct.TimesTriggered);

    }
    
}

Best Answer chosen by Shawn Reichner 29
Shawn Reichner 29Shawn Reichner 29
Nevermind yall!  I saw in my test class the second account I was creating, was referencing the first Accounts fields and was messing it up.  Upon fixing that I now have 100 percent code coverage!  

All Answers

Shawn Reichner 29Shawn Reichner 29
Sorry, it is lines 54 through 64 and also lines 82 through 93 that are not being covered.  The rest of the class is covered. 

Thanks again for any hero out there that can help
Shawn Reichner 29Shawn Reichner 29
Nevermind yall!  I saw in my test class the second account I was creating, was referencing the first Accounts fields and was messing it up.  Upon fixing that I now have 100 percent code coverage!  
This was selected as the best answer