+ Start a Discussion
Nagarjuna Reddy.PNagarjuna Reddy.P 

Test class for simple apex class that inserts a record

Hi All,
           I have a simple method that inserts record into account object through lightning component.My apex class method is as follows.
 
 @AuraEnabled
     public static Account saveAccount(account acc){
       insert acc;
       return acc;
     }
My test class for this is

@isTest
private class testClassExample {
    @isTest static void createAcc(){
        Account acc=new Account();
        acc.Name = 'Test Account';
        acc.Phone = '12345';
        acc.Account_Type__c = 'Insurance';
        try{
             insert acc;
        }catch(Exception e){
            system.debug(e);
        }
                  
        Account a = example.saveAccount(acc); 
        integer count=[select count() from account];
        system.assertEquals( 1,count);
    }
My Test coverage is 66% and return statement from saveAccount method is not covered for coverage,how to cover this return statement.
Any help is appreciated.

Thank you.
Best Answer chosen by Nagarjuna Reddy.P
Khan AnasKhan Anas (Salesforce Developers) 
Hi Nagarjuna,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
@isTest
private class testClassExample {
    @isTest static void createAcc(){
        Account acc=new Account();
        acc.Name = 'Test Account';
        acc.Phone = '12345';
        acc.Account_Type__c = 'Insurance';
                  
        Account a = example.saveAccount(acc); 
        integer count=[select count() from account];
        system.assertEquals( 1,count);
    }

Please note that you don't have to insert the account in a test class.

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 Nagarjuna,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
@isTest
private class testClassExample {
    @isTest static void createAcc(){
        Account acc=new Account();
        acc.Name = 'Test Account';
        acc.Phone = '12345';
        acc.Account_Type__c = 'Insurance';
                  
        Account a = example.saveAccount(acc); 
        integer count=[select count() from account];
        system.assertEquals( 1,count);
    }

Please note that you don't have to insert the account in a test class.

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
Nagarjuna Reddy.PNagarjuna Reddy.P
Hi Khan,
                Thank you for your reply,now it works , thnaks for remainding me that we should not perform dml operations in test classes.