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 

Code coverage issue for soql query

HI Experts,

I have written a visual force component and an apex class, problem over here is that, i can able to cover upto 73%, and not able to cover soql queries , lines which i bolded those are the lines which i am not able to cover. can anyone help me  please below is my code.
 
public with sharing class CampaignApprovalReject {
    
    
  public String camIds {get;set;}
    public Campaign camp{get;set;}
    Public ProcessInstanceStep pStep{get;set;}
    Public String sfdcBaseURL{get;set;}
    
    
    public CampaignApprovalReject (){
        
    }
    public ProcessInstanceStep getApprovalSteps() {
       
        if (camIds != null) 
        {
           
            pStep = [Select SystemModstamp,StepStatus, ProcessInstanceId, OriginalActorId,OriginalActor.Name,
                     Comments, ActorId From ProcessInstanceStep where ProcessInstance.TargetObjectId=:camIds order by SystemModstamp desc limit 1];  
            system.debug('@@@pStep'+pStep); 
        }
        return pStep ;
    }
    
    Public campaign getcampaigns(){
        if (camIds != null) 
        {
            camp= [Select id,name,Type,Description__c,ParentName__c from campaign where id=:camIds limit 1];
            
        }
        
        return camp;
    }
 
    
    public List<ProcessInstanceHistory> getrecallApprovalSteps() 
    {
        if (camIds != null) 
        {
            camp= [Select Id, (Select TargetObjectId, SystemModstamp, StepStatus, RemindersSent, ProcessInstanceId, OriginalActorId, IsPending, IsDeleted, Id, CreatedDate, CreatedById, Comments, ActorId From ProcessSteps order by SystemModstamp desc) from campaign where Id = :camIds ];
            return camp.ProcessSteps;
        }
        return new List<ProcessInstanceHistory>();
    }
    
    Public String getURL(){
        if (camIds != null) {
            sfdcBaseURL = URL.getSalesforceBaseUrl().toExternalForm()+'/'+camp.id;
        }
        return sfdcBaseURL;
    }
    
}

Below is my test class.
 
@isTest
public class Campaign_Test {
    @isTest static void myTest() 
    {
        
        Campaign a = new Campaign ();
        a.Name='Example';
        a.Description__c='test';
        a.Status='Planned';
        a.StartDate=system.today();
        a.CurrencyIsoCode='USD';
        a.Parent__c=true;
        a.type='Nurture';
        insert a;
        Campaign a2=[Select id,name,Type,CAP_WT_Campaign_Short_Description__c from campaign where name='Example' LIMIT 1];
        System.assert(a2!=null);        
        Approval.ProcessSubmitRequest req1 =  new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(a.id);
        req1.setProcessDefinitionNameOrId('Example_Process');
        req1.setSkipEntryCriteria(true);
        CampaignApprovalReject cmp = new CampaignApprovalReject();
        ProcessInstanceStep  process = cmp.getApprovalSteps();
        Campaign itemObj = cmp.getcampaigns();
        list<ProcessInstanceHistory> PI=cmp.getrecallApprovalSteps();
        string itemob=cmp.getURL();
        cmp.camIds='Example';
        System.assertEquals('Example', a.Name);
        
    }
}

 
SwethaSwetha (Salesforce Developers) 
HI Aditya,
Based on what you highlighted, it appears that if(camIds != null) is not getting covered. I see your testclass having the line cmp.camIds='Example';
Can you setup debug logs to see where the value is getting passed as null for camIds to identify the issue?Thanks
The new LearnerThe new Learner
HI Swetha,

I dont know how to pass that camids so i added like that actually that camids will get from the Visual component below.

<apex:component controller="CampaignApprovalReject" access="global"> <apex:attribute name="camId" assignTo="{!camIds}" type="String" description="Id of the cont"/>
AnudeepAnudeep (Salesforce Developers) 
Can you add the following in your test class and let me know if it helps?
 
CampaignApprovalReject cmp = new CampaignApprovalReject(); 
cmp.camIds = 'Example';

Basically, if it is a class-level variable, then you need to call it the way it is specified above in your test class with an instance of the class and dot notation.

If it is a private variable then you should add @TestVisible before the variable.

To learn more about the annotation, please check the following link 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm

If you find this information helpful, please mark this answer as Best. It may help others in the community

Regards, 
Anudeep
The new LearnerThe new Learner
hi anudeep,

Its already there in the test class. kindly have a look
AnudeepAnudeep (Salesforce Developers) 
Yeah, I looked at it before posting my answer. However, I still think you should set the variable in the below fashion because it is a class-level variable. Let me know if adding this code doesn't help

CampaignApprovalReject cmp = new CampaignApprovalReject(); cmp.camIds = 'Example';