0

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.

1 Answer 1

1

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)

3
  • Thanks for your comment. In my case, i am only calling the first method which automatically calls the second method. I do not have any control over it. How to write test classes in those scenario?
    – nano
    Jul 22, 2020 at 7:07
  • You've to prepare test data in such a way that all related logic evaluates properly (positive test case/negative test case). So you'll have to create custom setting data.
    – mritzi
    Jul 22, 2020 at 7:12
  • Thanks. Let me try it and will let you know.
    – nano
    Jul 22, 2020 at 7:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .