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
Somnath Paul 3Somnath Paul 3 

Test Class for Object Clone

Hi,
I am trying to write a Test Class for a Queueable apex class but the code coverage is not reaching 100%.

Queueable class:
public class AddPrimaryContact implements Queueable{
    private Contact con;
	private string state;
    public AddPrimaryContact(Contact con,String state)
    {
        this.con=con;
        this.state = state;
    }
    public void execute(QueueableContext Context)
    {
        Contact cont = [select Id,LastName,AccountId from Contact where Id=: con.Id limit 1];
        List<Account> accntlist = [select Id,Name,BillingState from Account where BillingState=: state limit 200];
        List<Contact> Conlist = new List<Contact>();
        for(Account a: accntlist)
        {	
            Contact cclone = cont.clone(false,false,false,false);
            cclone.AccountId = a.Id;
            Conlist.add(cclone);
        }
        insert Conlist;
    }
}

Test Class:
@isTest(seeAllData=false)
private class AddPrimaryContactTest {

    @testSetup
    static void setup()
    {
        List<Account> testAcnlist = new List<Account>();
        for(Integer i=0;i<50;i++)
        {
            Account a = new Account(Name='Test'+i,BillingCity='NY');
            testAcnlist.add(a);
        }
        for(Integer i=0;i<50;i++)
        {
            Account a = new Account(Name='Test'+i,BillingCity='CA');
            testAcnlist.add(a);
        }
        insert testAcnlist;
       Contact cnt = new Contact(LastName='TestContact');
        insert cnt;
    }
    static testmethod void testenqueuebale()
    {
         Contact conobj = [Select Id,Name,LastName from Contact where LastName='TestContact'];
        AddPrimaryContact apc = new AddPrimaryContact(conobj,'CA');
        Test.startTest();
        system.enqueueJob(apc);
        Test.stopTest();
        //system.assertNotEquals(null, [select Id,(Select LastName from contacts) from Account where BillingCity='CA']);
    }
}

Test Execution:
Strike through lines are not covered. Appreciate your help !!

public class AddPrimaryContact implements Queueable{
    private Contact con;
    private string state;
    public AddPrimaryContact(Contact con,String state)
    {
        this.con=con;
        this.state = state;
    }
    public void execute(QueueableContext Context)
    {
        Contact cont = [select Id,LastName,AccountId from Contact where Id=: con.Id limit 1];
        List<Account> accntlist = [select Id,Name,BillingState from Account where BillingState=: state limit 200];
        List<Contact> Conlist = new List<Contact>();
        for(Account a: accntlist)
        {    
            Contact cclone = cont.clone(false,false,false,false);
            cclone.AccountId = a.Id;
            Conlist.add(cclone);

        }
        insert Conlist;
    }
}

Regards
Somnath
Best Answer chosen by Somnath Paul 3
Dilip_VDilip_V
Somnath,

You havn't assigned billingstate value for accounts in test class.
try this test class
 
@isTest(seeAllData=false)
private class AddPrimaryContactTest {

    @testSetup
    static void setup()
    {
        List<Account> testAcnlist = new List<Account>();
        for(Integer i=0;i<50;i++)
        {
            Account a = new Account(Name='Test'+i,BillingCity='NY');
            testAcnlist.add(a);
        }
        for(Integer i=0;i<50;i++)
        {
            Account a = new Account(Name='Test'+i,BillingCity='CA');
            a.billingstate='CA';
            testAcnlist.add(a);
        }
        insert testAcnlist;
       Contact cnt = new Contact(LastName='TestContact');
        insert cnt;
    }
    static testmethod void testenqueuebale()
    {
         Contact conobj = [Select Id,Name,LastName from Contact where LastName='TestContact'];
        AddPrimaryContact apc = new AddPrimaryContact(conobj,'CA');
        //apc.state='CA';
        Test.startTest();
        system.enqueueJob(apc);
        Test.stopTest();
        //system.assertNotEquals(null, [select Id,(Select LastName from contacts) from Account where BillingCity='CA']);
    }
}

Let me know if you have any issues.

Mark it as best answer if it works.

Thanks.

All Answers

Dilip_VDilip_V
Somnath,

You havn't assigned billingstate value for accounts in test class.
try this test class
 
@isTest(seeAllData=false)
private class AddPrimaryContactTest {

    @testSetup
    static void setup()
    {
        List<Account> testAcnlist = new List<Account>();
        for(Integer i=0;i<50;i++)
        {
            Account a = new Account(Name='Test'+i,BillingCity='NY');
            testAcnlist.add(a);
        }
        for(Integer i=0;i<50;i++)
        {
            Account a = new Account(Name='Test'+i,BillingCity='CA');
            a.billingstate='CA';
            testAcnlist.add(a);
        }
        insert testAcnlist;
       Contact cnt = new Contact(LastName='TestContact');
        insert cnt;
    }
    static testmethod void testenqueuebale()
    {
         Contact conobj = [Select Id,Name,LastName from Contact where LastName='TestContact'];
        AddPrimaryContact apc = new AddPrimaryContact(conobj,'CA');
        //apc.state='CA';
        Test.startTest();
        system.enqueueJob(apc);
        Test.stopTest();
        //system.assertNotEquals(null, [select Id,(Select LastName from contacts) from Account where BillingCity='CA']);
    }
}

Let me know if you have any issues.

Mark it as best answer if it works.

Thanks.
This was selected as the best answer
Somnath Paul 3Somnath Paul 3
Thanks Dilip for your help. It was my mistake.
:)