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
Sujit KarandeSujit Karande 

Check string value in test class

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?
 
Sohan Raj GuptaSohan Raj Gupta
Hi Sujit,

You should wirte both IF and ELSE condition in 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'; 
}

In this case your both condition will run in test class. Hope this will help you.

Please let me know if it helped or you need any more assistance.

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
Sujit KarandeSujit Karande
Thanks for your response, Sohan!

But unfortunately, it doesn't work. 
Jainam ContractorJainam Contractor
Hi Sujit,

Can you please try the below code and let me know if it helps you:

Opportunity opp = [select Deal_Type__c from opportunity where Id: = <some id>]; 
Case objCase = new Case(); 
Case objCase1 = 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'; 
}

objCase1.standard_or_nonstandard__c = 'No';

if(objCase1.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'; 
}

Please let me know if it works. 

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Sujit KarandeSujit Karande
It doesn't work, Jainam! 

It reduces the code coverage by 1%
Jainam ContractorJainam Contractor
Hi Sujit,

Is the above code your test class or is it the actual class and you want a test class for the same.

Can you please share your test class and the actual class...

Thanks,
Jainam Contractor
Sujit KarandeSujit Karande
Thanks Jainam for your valuable time on this.

The code in question is of the test class. Below is the code in actual class
List<case> form;
form = [select standard_or_nonstandard__c from case where opportunityId := <some id>]

if(form[0].standard_or_nonstandard__c = 'Yes'){ 
    opp.Deal_Type__c = 'Standard'; 
}
 else{                                      
     opp.Deal_Type__c = 'Not Standard'; 
}

 
Jainam ContractorJainam Contractor
Hi Sujit,

You can insert some dummy records in Test Class and then call those same records in the Class Method.

Assume Class Name : CaseOpp
Method Name : FetchCase

Below would be the Test Class:
@isTest
public class TestCaseOpp{
    
    @isTest
    public static void TestFetchCase(){
        Opportunity Opp = new Opportunity(Name = 'Test Opp', CloseDate = Date.today());
        Opportunity Opp1 = new Opportunity(Name = 'Test Opp1', CloseDate = Date.today());
        insert Opp;
        insert Opp1;
        Case C1 = new Case(Status = 'New', Origin = 'Web', OpportunityId = Opp.Id, standard_or_nonstandard__c = 'Yes');
        Case C2 = new Case(Status = 'New', Origin = 'Web', OpportunityId = Opp1.Id, standard_or_nonstandard__c = 'No');
        insert C1;
        insert C2;
        CaseOpp.FetchCase(Opp.Id); //Assuming Method FetchCase is static
        CaseOpp.FetchCase(Opp1.Id); //Assuming Method FetchCase is static
    }
}

Please check and let me know if it works.

Thanks,
Jainam Contractor​