0 votes
1 view
in Salesforce by (11.3k points)

Below is the code for my test class.

Opportunity opp = [select Deal_Type__c from opportunity where Id: = <some id>]; 

Case objCase = new Case(); 

objCase.standard_or_nonstandard__c = 'Yes';

if(objCase.standard_or_nonstandard__c = 'Yes'){ // this if is getting tested 

    opp.Deal_Type__c = 'Standard'; 

}

 else{                                          // else part is getting skipped

     opp.Deal_Type__c = 'Not Standard'; 

}

And only first if condition is getting tested and other is skipping which is why the code is not reaching 75% off code coverage.

the field standard_or_nonstandard__c is picklist having two values Yes & No.

And if the value if Yes, the deal type should be standard, and if No, the deal type is not standard.

Any suggestion on this?

1 Answer

0 votes
by (28.6k points)

You should write both IF and ELSE condition in a test like below sample:

Opportunity opp = [select Deal_Type__c from opportunity where Id: = <some id>]; 

Case objCase = new Case(); 

objCase.standard_or_nonstandard__c = 'Yes';

if(objCase.standard_or_nonstandard__c = 'Yes'){ // this if is getting tested 

    opp.Deal_Type__c = 'Standard'; 

}

 else{                                          // else part is getting skipped

     opp.Deal_Type__c = 'Not Standard'; 

}

objCase.standard_or_nonstandard__c = 'No';

if(objCase.standard_or_nonstandard__c = 'Yes'){ // this if is getting tested 

    opp.Deal_Type__c = 'Standard'; 

}

 else{                                          // else part is getting skipped

     opp.Deal_Type__c = 'Not Standard'; 

}

...