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
The new LearnerThe new Learner 

How to cover If condition in test class

Hi Experts,
 
I am struggling to cover the below if condition line( if(minutes <60)) in the test class can anyone help me out, please?
 
for(campaignmember cm:listcamps)
        {
            Long customFieldLong = cm.CreatedDate.getTime();
            Long dt2Long = DateTime.now().getTime();
            Long milliSeconds = dt2Long - customFieldLong;
            Long seconds = milliSeconds / 1000;
            Long minutes = seconds / 60;
            
            if(cm.status=='Form' || cm.status=='submit')
            {
                if(minutes<60)
                {
                    cammemleadId.add(cm.leadId);
                }

 
Best Answer chosen by The new Learner
Jaya Karthik  karnatiJaya Karthik karnati

Thanks for providing the Code,

i have replicated the code in my personal Org and based on my debugging the issue is not with the if condition in minute , the code is not even reaching till the if condition.

The logic was not satisfied in the above if condition it self (i.E : if(cm.status=='Form' || cm.status=='submit') )

On further debugging : the issue was even tough we are trying to place values in picklist value of status : since we are using OOTB status field the the picklist value of form is not getting stamped and , the default picklist value of sent is getting stamped in my case.

Debug SS:
User-added image

Campaign member status field values :

User-added image

Can you kindly provide the picklist values for your campain member object : status field

FYI , i changed the condition in if clause to status=sent and the code coverage reached 100%.

User-added image

Thank you ,
Karthik

 

All Answers

Jaya Karthik  karnatiJaya Karthik karnati
Hello ,

Based on your code , you are validating and capturing only the data modified in last hour. So any test data created VIA test class must be eligible for your class . could you please share the query for listcamps  and also the test class you used .

When i tried to execute your logic it covered for me inside the if condition . The reason being in test class i created the account so the createddate will be latest.

User-added image

Test class :
@istest
public class AccountMinuteIfconditionTest {
@istest
    public static void unittest(){
         Account acc = new Account(Name='Test Account');  
        Insert acc;
        Test.startTest();  
        AccountMinuteIfcondition.minuteif(acc.Id);
        Test.startTest();
        
    }
}

Thank you,
karthik
PriyaPriya (Salesforce Developers) 

Hi 

 

Refer these examples :-
https://developer.salesforce.com/forums/?id=906F000000091SnIAI
https://developer.salesforce.com/forums/?id=9060G0000005NN9QAM

 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan

 

The new LearnerThe new Learner
HI @Jaya Karthik karnati : listcamps is a Trigger.New which I am passing from the trigger. Below is the test class I am writing
 
lead.RecordTypeId =Schema.SObjectType.Lead.getRecordTypeInfosByDeveloperName().get('LeadRT').getRecordTypeId();
               
                lead.Status = 'Qualified';   
                lead.campaign__c= campaign.Id;
                insert lead;
                
               CampaignMember newMember = new CampaignMember(Leadid= lead.Id, Status = 'Form', CampaignId = campaign.Id);
               insert newMember;

 
The new LearnerThe new Learner
its direct handler class method and passing list of campaignmembers as Trigger.new(listcamps) and doing the below calculation
 
public class campaignmemberhnalder
{
    public static void campaignmembermethod(list<campaignmember>listcamps)
    {
        set<Id> cammemleadId = new set<Id>(); 
        
        for(campaignmember cm:listcamps)
        {
            Long customFieldLong = cm.CreatedDate.getTime();
            Long dt2Long = DateTime.now().getTime();
            Long milliSeconds = dt2Long - customFieldLong;
            Long seconds = milliSeconds / 1000;
            Long minutes = seconds / 60;
            
            if(cm.status=='Form' || cm.status=='submit')
            {
                if(minutes<60)
                {
                    cammemleadId.add(cm.leadId);
                }
                
            }
            
        }

 
Jaya Karthik  karnatiJaya Karthik karnati

Thanks for providing the Code,

i have replicated the code in my personal Org and based on my debugging the issue is not with the if condition in minute , the code is not even reaching till the if condition.

The logic was not satisfied in the above if condition it self (i.E : if(cm.status=='Form' || cm.status=='submit') )

On further debugging : the issue was even tough we are trying to place values in picklist value of status : since we are using OOTB status field the the picklist value of form is not getting stamped and , the default picklist value of sent is getting stamped in my case.

Debug SS:
User-added image

Campaign member status field values :

User-added image

Can you kindly provide the picklist values for your campain member object : status field

FYI , i changed the condition in if clause to status=sent and the code coverage reached 100%.

User-added image

Thank you ,
Karthik

 

This was selected as the best answer
The new LearnerThe new Learner
Hi , those are the values  and there is no default value is available from my end.
Jaya Karthik  karnatiJaya Karthik karnati
Hi ,

Those are the values means ? can you please provide the screenshot for what values are present in ur org?.

and also add the 
system.debug('cm.status-->'+cm.status);
in line you class after long minutes and kindly provide the value its printed after running the test class.

Thanks,
Karthik
 
The new LearnerThe new Learner
HI Karthik,

your absolutely right , for that I have inserted the below the record and then code coverage got improved. and kudos for ur efforts
 
CampaignMemberStatus statusOption = new CampaignMemberStatus(); statusOption.Label = 'Form'; statusOption.CampaignId = campaign.Id; statusOption.IsDefault = false; insert statusOption;