2

I tried declaring an empty string named tocheck, and I passed this variable to my method. This should cover the else clause in my code. However, this snippet is not covered. What else can I try to provide code coverage of my conditional statement?

Full Coverage, except on <code>else</code> branch

public static String sourceConfidence(String sourceSystemID){
        system.debug('sourceSystemID'+sourceSystemID);
        if(sourceSystemID != '' ||sourceSystemID != null){
            try{
                Source_System_Configuaration__c  result = [SELECT Source_Confidence__c FROM Source_System_Configuaration__c WHERE System_Id__c= :sourceSystemID];
                sourceConfidence=Integer.valueOf(result.Source_Confidence__c);

            }catch(Exception e){
                system.debug(e);
                return '0';
        }
        }else {
        return '0';
        }


    return String.ValueOf(sourceConfidence);

    }
5

It looks like a condition ambiguity in the code. You want to prevent null and blank to enter in main code.

But if(sourceSystemID != '' ||sourceSystemID != null) will not work. If you pass a null, it will make the first condition true and let the code enter in main code. If you pass blank then it will make second condition true and let the code enter in main code.

I will suggest to change the condition as:

if(sourceSystemID != ''  && sourceSystemID != null) // check with AND
{
   // bla
}


Kieth C's suggestion (better):

if(!String.isEmpty(sourceSystemID)) // check both null and blank
{
   // bla
}


Now you can pass:

ClassName.sourceConfidence(null); // This
ClassName.sourceConfidence(''); // or this
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.