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
Navya sree 4Navya sree 4 

Test class coverage help?

public class ContactpQueueable implements Queueable {
    public List<Contact> conToInsert = new List<Contact>();
    public List<Contact> conToDeleteList = new List<Contact>();
    
    
    public void execute(QueueableContext context) {
        if(!conToInsert.isEmpty()){
            Database.insert(conToInsert );
        }
        if(!conDeleteList.isEmpty()){
            Database.delete(conToDeleteList);
        }
    }
}
Best Answer chosen by Navya sree 4
Raju yadavRaju yadav
Hi Navya,
Please update the test class as follow
@isTest
public class ContactpQueueableTest {
	
    @isTest static void ContactpQMethod(){
        
        List<Contact> conList = new List<Contact>();
        Contact thisContact = new Contact();
        thisContact.LastName = 'testContact';
        conList.add(thisContact);
        
        
        Test.startTest();
        	ContactpQueueable thisQueueable = new ContactpQueueable();
        	thisQueueable.conToInsert = conList;
        	thisQueueable.conToDeleteList = conList;
        	System.enqueueJob(thisQueueable);
        	
        Test.stopTest();
    }
}

It will give you 100% code coverage.
Thanks​​​​​​​

All Answers

Raju yadavRaju yadav

Hi Navya,

You can try below code for the test class
 

@isTest
public class ContactpQueueableTest {
	
    @isTest static void ContactpQMethod(){
        
        List<Contact> conList = new List<Contact>();
        Contact thisContact = new Contact();
        thisContact.LastName = 'testContact';
        conList.add(thisContact);
        insert conList;
        
        
        
        Test.startTest();
        	ContactpQueueable thisQueueable = new ContactpQueueable();
        	System.enqueueJob(thisQueueable);
        Test.stopTest();
    }
Navya sree 4Navya sree 4
Thank you Raju,
I covered only 71% percent.
 Database.insert(conToInsert );
Database.delete(conToDeleteList);

two line not covered.

Thanks 
Navya
Raju yadavRaju yadav
Hi Navya,
Please update the test class as follow
@isTest
public class ContactpQueueableTest {
	
    @isTest static void ContactpQMethod(){
        
        List<Contact> conList = new List<Contact>();
        Contact thisContact = new Contact();
        thisContact.LastName = 'testContact';
        conList.add(thisContact);
        
        
        Test.startTest();
        	ContactpQueueable thisQueueable = new ContactpQueueable();
        	thisQueueable.conToInsert = conList;
        	thisQueueable.conToDeleteList = conList;
        	System.enqueueJob(thisQueueable);
        	
        Test.stopTest();
    }
}

It will give you 100% code coverage.
Thanks​​​​​​​
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Navya,
Try the below code it works for me and cover the 100% code:
@isTest
public class ContactpQueueableTest {
    
    @isTest static void ContactpQMethod(){
        
        List<Contact> conList = new List<Contact>();
        Contact contactObj = new Contact();
        contactObj.LastName = 'testContact';
        conList.add(contactObj);
        Test.startTest();
            ContactpQueueable thisQueueable = new ContactpQueueable();
                thisQueueable.conToInsert = conList;
            System.enqueueJob(thisQueueable);
        Test.stopTest();
    }
    @isTest static void ContactpQMethodSecond(){
        List<Contact> conList = new List<Contact>();
        List<Contact> conToDeleteList = new List<Contact>();
        Contact contactObj = new Contact();
        contactObj.LastName = 'testContact';
        insert contactObj;
        conToDeleteList = [SELECT LastName From Contact Where Id =: contactObj.Id ];
        Test.startTest();
            ContactpQueueable thisQueueable = new ContactpQueueable();
            thisQueueable.conToDeleteList = conToDeleteList;
            System.enqueueJob(thisQueueable);
        Test.stopTest();
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi