TransWikia.com

Unit Test - When one Test Method called another Test Method within the same class

Salesforce Asked by nano on November 20, 2021

I have a scenario in which i am not sure how to write Unit Tests, Please assist me on this.

Public class FilePreview{
    public static fileClass getFilefromAWS(String str){
        //Logic for GetFileFromAWS
        //This Method further called another method 
        
        fileClass = getFile(String str, Object objName);
    }
    
    //fileClass is a wrapper having many variables
}
  

The inner method getFile() uses custom setting which contains bucket name of S3 and other variables. I made SeeAllData = true for accessing the custom settings.

So now, i have few else conditions saying if the bucket name is not present throw an error, if the fields in custom settings are not present throw an error. Those particular lines in the else statement are not being covered.

Below are some of the else statements : result is an instance of wrapper class

else {
        result.bError = true;
        result.stMessage = 'Details are empty'; // When bucket name is blank
                   }
            } else {
                result.bError = true;
                result.stMessage = 'Details are empty'; // When custom setting instance is null
            }
        } catch (Exception e) {
            result.bError = true;
            result.stMessage = e.getMessage();
        }

Can anyone of you help me how to cover those?

Thanks in advance.

One Answer

In your test method, you can create new records in custom setting, just as will create test data for other standard/custom object.

At the beginning of your test method, insert a new record of custom setting (Api will be like: < MyCustomSetting >__c), populate the fields values as per the method you are testing, and it will work.

Do not use seeAllData=true for test classes, (except in extremely rare cases where creating exact data is near to impossible)

Answered by mritzi on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP