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
Nitish Pandey 14Nitish Pandey 14 

Queueable apex Test Class

Hello,

I am facing issue in Trailhead : Queueable Apex
https://trailhead.salesforce.com/asynchronous_apex/async_apex_queueable

We have to write a class and test class for 100% code coverage.
But on Validating, I am getting error: 

Challenge Not yet complete... here's what's wrong: 
The 'AddPrimaryContactTest' test class doesn't appear to be using the 'AddPrimaryContact' class.


He are  my classes:
global class AddPrimaryContact implements Queueable {
    
    private Contact queueContact;
    private String contactState;
    
    public AddPrimaryContact(){}
    
    public AddPrimaryContact(Contact con, String state){
        queueContact = con;
        contactState = state;
    }
    
    public void execute(QueueableContext context){
        
        List<Account> accList = [select id,name from Account  where BillingState =: contactState limit 200];
        List<Contact>conList = new List<Contact>();
        for(account acl : accList){
            Contact cont = queueContact.clone(false);
            cont.AccountId=acl.id;
            conList.add(cont);
        }
        insert conList;
        
    }
}

Test class:
@isTest
public class AddPrimaryContactTest {
    
    @testSetup
    public static void setup(){
        
        List<Account> accList = new List<Account>();
        for(integer ing =0;ing<50; ing++){
            Account acc = new Account(Name='TEst Acc',BillingState ='NY');
            accList.add(acc);
        }    
        
        for(integer ing =0;ing<50; ing++){
            Account acc = new Account(Name='TEst Acc',BillingState ='CA');
            accList.add(acc);
        } 
        
        insert accList;
        
        Contact con = new Contact(LastName='TestContact');
        insert con;
    }
    
   static testmethod  void testAcount(){
        
        contact con = [select id,name,AccountId from contact where name='TestContact'];
         Test.startTest();
         
        AddPrimaryContact ad = new AddPrimaryContact ();
        AddPrimaryContact updater = new AddPrimaryContact (con, 'CA');
        // startTest/stopTest block to force async processes to run
               System.enqueueJob(ad);
        ID jobID = System.enqueueJob(updater);
        Test.stopTest();       
        
    }
}
I am getting 100% code coverage for my class.
Best Answer chosen by Nitish Pandey 14
SandhyaSandhya (Salesforce Developers) 
Hi Nitish,

I cleared the same error by removing the space in test class for below line.
 
AddPrimaryContact updater = new AddPrimaryContact (con, 'CA');

 it should be 

        AddPrimaryContact updater = new AddPrimaryContact(con, 'CA');

Please add this second line in your test class and you will pass the challenge.

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya



 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi Nitish,

I cleared the same error by removing the space in test class for below line.
 
AddPrimaryContact updater = new AddPrimaryContact (con, 'CA');

 it should be 

        AddPrimaryContact updater = new AddPrimaryContact(con, 'CA');

Please add this second line in your test class and you will pass the challenge.

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya



 
This was selected as the best answer
Nitish Pandey 14Nitish Pandey 14
Thanks Sandhya,
It worked.
XIN JINXIN JIN
Thank you so much Sandhya!
Worked for me!
ManjusrinuManjusrinu

Hi Sandhya,

Even i too got the same error ,but i have modified as you described ,but still i am facing the issue,

Can you help me to solve it out ?....

global class AddPrimaryContact implements Queueable {
    private contact  conRecords;
    private string name;

    public AddPrimaryContact(contact recordname,string state){
        this.conRecords = recordname;
        this.name= state; 
    }
    public void execute(QueueableContext context){
   List<Account> accountlist= new List<Account>();
        accountlist = [select id,Name,
                       BillingState from Account where BillingState = : name LIMIT 200];
      List<Contact> contactlist = new List<Contact>();
        for(Account acc : accountlist){
            Contact con = conRecords.clone(false,false,false,false);
            contactlist.add(con);
        } 
            insert contactlist; 
        
    }

}

Test class :

@istest
public class AddPrimaryContactTest {
    static testmethod void setup() {
        List<Account> accountsWithstates = new List<Account>();
        for(Integer i=1 ; i<=50 ; i++){
            Account acc = new Account();
            acc.Name = 'test[i]'; 
            acc.BillingState= 'NY'; 
            accountsWithstates.add(acc);
        }
        for(Integer i=1 ;i<=50 ; i++) {
            Account acc1 = new Account();
            acc1.Name = 'suziki' + i;
            acc1.BillingState = 'CA';
            accountsWithstates.add(acc1); 
        }
        insert accountsWithstates;
        Contact newContact = new Contact ();
        newContact.FirstName = 'MyFirstName';
        newContact.LastName = 'MyLastName';
        newContact.Email = 'myname@mydomain.com';
insert newContact;
        Test.startTest();
        AddPrimaryContact updater = new AddPrimaryContact(newContact, 'CA');
        system.enqueueJob(updater);
        Test.stopTest();
    }
}

The highlted lines are not covered .