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
RaffusRaffus 

How to cover else part of this method in test class

This is a method in which the else part is not getting covered -
" else {
                rand_App_Customer__c customer = new rand_App_Customer__c(); " 
 
@AuraEnabled
    public static rand_App_Customer__c CustomerInfo(String partyId, String mobile) {
        try {
            String query = 'SELECT Id, Customer_Last_Name__c, Customer_Email__c, Nationality__c, Customer_First_Name__c, Date_of_Birth__c, Customer_Name__c, Party_Id__c FROM rand_App_Customer__c WHERE ';
            if (String.isNotBlank(partyId)) {
                query += ' Customer_Name__c != null AND Party_Id__c=:partyId';
            }
            else if (String.isNotBlank(mobile)) {
                query += ' Customer_Mobile__c=:mobile';
            }
            List<rand_App_Customer__c> customers = Database.query(query + ' LIMIT 1');
            if (customers.size() > 0) {
                System.debug('customers ' + customers);
                if (String.isNotBlank(partyId)) {
                    List<Account> accs = [SELECT Id, FirstName, LastName, email__c, Mobile__c FROM Account WHERE Party_Id__c = :partyId ORDER BY CreatedDate DESC LIMIT 1];
                    if (!accs.isEmpty()) {
                        customers[0].Customer_First_Name__c = accs[0].FirstName;
                        customers[0].Customer_Last_Name__c = accs[0].LastName;
                        customers[0].Customer_Email__c = accs[0].email__c;
                        customers[0].Customer_Mobile__c = accs[0].Mobile__c;
                        customers[0].recalculateFormulas();
                        customers[0].Name = customers[0].Customer_Name__c;
                        update customers[0];
                    }
                }
                return customers[0];
            }
            else {
                rand_App_Customer__c customer = new rand_App_Customer__c();
                customer.Party_Id__c = partyId;
                if (String.isNotBlank(partyId)) {
                    List<Account> accs = [SELECT Id, FirstName, LastName, email__c, Mobile__c FROM Account WHERE Party_Id__c = :partyId ORDER BY CreatedDate DESC LIMIT 1];
                    if (!accs.isEmpty()) {
                        customer.Customer_First_Name__c = accs[0].FirstName;
                        customer.Customer_Last_Name__c = accs[0].LastName;
                        customer.Customer_Email__c = accs[0].email__c;
                        customer.recalculateFormulas();
                    }
                }
                customer.Customer_ID__c = String.isNotBlank(partyId) ? partyId : mobile;
                if (String.isNotBlank(mobile)) {
                    customer.Customer_Mobile__c = mobile;
                }
                customer.Name = String.isBlank(customer.Customer_Name__c) ? 'User' : customer.Customer_Name__c;
                upsert customer Customer_ID__c;
                return customer;
  
            }
        }
        catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
Here is my written test method
@isTest
    public static void getCustomerInfoTest(){
        
        rand_App_Customer__c randCustomer = new rand_App_Customer__c();
        randCustomer.Customer_ID__c = '1234567';
        randCustomer.Customer_First_Name__c = 'Test rand';
        randCustomer.Customer_Last_Name__c = 'Customer';
        randCustomer.Party_Id__c = '8545254752';
        randCustomer.Nationality__c = 'India';
        // randCustomer.Date_of_Birth__c = ;
        randCustomer.Customer_Mobile__c = '8545254752';
        randCustomer.Customer_Email__c = 'tannow@tan.ae';
        Insert randCustomer;

        Account acc = new Account();
            
            acc.FirstName = 'TestFname';
            acc.LastName = 'TestLname';
            acc.Mobile__c = '8545254752';
            acc.Party_Id__c = '8545254752';
            insert acc;

        randAppController.getCustomerInfo('8545254752', '8545254752' );


 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class.
 
@isTest
    public static void getCustomerInfoTest(){
        
        rand_App_Customer__c randCustomer = new rand_App_Customer__c();
        randCustomer.Customer_ID__c = '1234567';
        randCustomer.Customer_First_Name__c = 'Test rand';
        randCustomer.Customer_Last_Name__c = 'Customer';
        randCustomer.Party_Id__c = '8545254752';
        randCustomer.Nationality__c = 'India';
        // randCustomer.Date_of_Birth__c = ;
        randCustomer.Customer_Mobile__c = '8545254752';
        randCustomer.Customer_Email__c = 'tannow@tan.ae';
        Insert randCustomer;

        rand_App_Customer__c randCustomer1 = new rand_App_Customer__c();
        randCustomer1.Customer_ID__c = '12345675';
        randCustomer1.Party_Id__c = '85452547522';
        randCustomer1.Nationality__c = 'India';
        // randCustomer.Date_of_Birth__c = ;
        randCustomer1.Customer_Mobile__c = '8545254752';
        randCustomer1.Customer_Email__c = 'tannow@tan.ae';
        Insert randCustomer1;

        Account acc = new Account();
            
            acc.FirstName = 'TestFname';
            acc.LastName = 'TestLname';
            acc.Mobile__c = '8545254752';
            acc.Party_Id__c = '8545254752';
            insert acc;
            Account acc1 = new Account();
            acc1.FirstName = 'TestFname';
            acc1.LastName = 'TestLname';
            acc1.Mobile__c = '8545254752';
            acc1.Party_Id__c = '85452547522';
            insert acc1;

        randAppController.getCustomerInfo('8545254752', '8545254752' );
        randAppController.getCustomerInfo('85452547522', '8545254752' );

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,