Activity Forums Salesforce® Discussions How to write a test class for a Queueable apex?

  • Shweta

    Member
    August 12, 2020 at 4:24 pm

    Queueable Apex Class:
    public class AddPrimaryContact implements Queueable{ private Contact c; private String state; public AddPrimaryContact(Contact c,String state){ this.c = c; this.state = state; } public void execute(QueueableContext context){ List<Account> acctlist=[Select id,name,(Select id,firstname,lastname from Contacts) from Account where BillingState =:state limit 200]; List<Contact> conlist = new List<Contact>(); for(Account acc :acctlist){ Contact con = c.clone(false,false,false,false); con.AccountId = acc.Id; conlist.add(con); } if(conlist.size()>0){ insert conlist; } }}Queueable Apex test Class:@isTestpublic class AddPrimaryContactTest { @isTest static void TestList() { List<Account> Teste = new List <Account>(); for(Integer i=0;i<50;i++) { Teste.add(new Account(BillingState = 'CA', name = 'Test'+i)); } for(Integer j=0;j<50;j++) { Teste.add(new Account(BillingState = 'NY', name = 'Test'+j)); } insert Teste; Contact co = new Contact(); co.FirstName='demo'; co.LastName ='demo'; insert co; String state = 'CA'; AddPrimaryContact apc = new AddPrimaryContact(co, state); Test.startTest(); System.enqueueJob(apc); Test.stopTest(); } }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos