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
Sarah RobertsonSarah Robertson 

Help test class for aura component

Hi guys 

I'm wondering if someone can help me i'm quite a newbie developer
I've created a button which is updating a field on the account  using a component and class. I'm having trouble gettting any coverage for my class can anyone tell me where i am going wrong ? 

in my test class i'm getting an error on "    updateBillingAddressPlan.UpdateBill(Acc);
" as it doesn't recognise Acc as a variable. 

Here is my class 

public class updateBillingAddressPlan {

    @AuraEnabled

    public static void UpdateBill(){
      
        list<Account> Acc_list = new list<Account>();

        Acc_list = [select id,Shipping_to_Billing__c from Account Limit 1];

        Account acc =new Account();

       acc.id=Acc_list[0].id;

        acc.Shipping_to_Billing__c=true;

        update acc;

    }

}

Test class 

@isTest

    
  
private class updateBillingAddressPlantest{
   @AuraEnabled
        public static boolean UpdateBill(){
        
        //List<Account> Acc_list = new list<Account>();
        
       // Acc_list = [select id,Shipping_to_Billing__c from Account Limit 1];
      //  system.debug(Acc_list.size());  
        Account acc =new Account();

      // acc.id=Acc_list[0].id;
        acc.name ='test account';
        acc.Siret_Number__c='12345678911111';
        acc.Shipping_to_Billing__c=true;

        insert acc;
        system.debug('inserting account was a success');
        return acc.Shipping_to_Billing__c;
        }
        
        
        static testmethod void doTest(){
        Test.startTest();
        User U = new User();
        U.FirstName = 'Testy';
        U.LastName = 'Tester';
      //  U.CurrencyIsoCode = 'GBP';
        U.Username = 'tester_User@test.com';
        U.email = 'tester@test.com';
        U.Alias = 'tstr';
        U.TimeZoneSidKey = 'Europe/London';
        U.LocaleSidKey ='en_GB';
        U.EmailEncodingKey ='ISO-8859-1';
        U.LanguageLocaleKey = 'en_US';
        U.profileID = '00e1t000000FMFdAAO';
       // U.Annual_Target_New_TCVP__c = 100000;
    //    U.Booked_YTD_New_TCVP__c = 50000;
        Insert U;
        
        System.runAs(U){
            Account Acc = new Account();
           // Acc.recordTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('UK Account').getRecordTypeId();
            Acc.Name = 'Account Testing';
            Acc.Siret_Number__c='12345678923456';
           // Acc.cf_customer_sector__c = 'Public';
        //    Acc.cf_customer_segment__c = 'SMB';
            Acc.cf_customer_vertical__c = 'Finance';
            insert Acc;
            
            Opportunity opp = new Opportunity();
            opp.name = 'Test Opportunity';
         //   opp.Start_Date__c = system.today();
            opp.closeDate = system.today().addDays(1);
            opp.stageName = 'Stage 2: Prospect';
            insert opp;}
            
            updateBillingAddressPlan.UpdateBill(Acc);

          
      
        Test.stopTest();
        
        
        
        
        
        }}
Best Answer chosen by Sarah Robertson
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sarah,

Greetings to you!

Please try the below code, it will give you 100% code coverage for above-mentioned class.
 
@isTest
public class Test_AuraTest {

    public static testmethod void test1(){
        
        Account acc = new Account();
        acc.Name = 'Test_123';
        acc.Shipping_To_Billing__c = true;
        INSERT acc;
        
        Test.StartTest(); 
        
			updateBillingAddressPlantest.UpdateBill();
			
		Test.StopTest();
    }
}

You are getting this error because you are passing a parameter to the method in test class but in the business class, there is no parameter for UpdateBill method.

Please refer to the below links which might help you further.

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sarah,

Greetings to you!

Please try the below code, it will give you 100% code coverage for above-mentioned class.
 
@isTest
public class Test_AuraTest {

    public static testmethod void test1(){
        
        Account acc = new Account();
        acc.Name = 'Test_123';
        acc.Shipping_To_Billing__c = true;
        INSERT acc;
        
        Test.StartTest(); 
        
			updateBillingAddressPlantest.UpdateBill();
			
		Test.StopTest();
    }
}

You are getting this error because you are passing a parameter to the method in test class but in the business class, there is no parameter for UpdateBill method.

Please refer to the below links which might help you further.

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Sarah RobertsonSarah Robertson
Thank you so much Khan that worked perfectly ! :) 
Ajay K DubediAjay K Dubedi
Hi Sarah,
Try this test class:
@isTest
private class updateBillingAddressPlan_Test {

     @isTest static void testupdateBillingAddressPlan() {
         try{
             Account ac = new Account();
             ac.Name = 'Test';
             ac.Shipping_to_Billing__c = False;
             //put all required fields here.
             insert ac;
             
             System.assertNotEquals(ac.Id, null,'Assertion Failed : Account Not Inserted');
             Test.startTest();
             updateBillingAddressPlan.UpdateBill();
             Test.stopTest();
         }
         Catch (Exception exp) {
            system.debug('The following exception has occurred in method "testGetPickListValues"  : ' + exp.getMessage() +
                         ' at Line no: ' + exp.getLineNumber());
        }
     }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi