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
St2018St2018 

Need help on writing unit test for this method?

Im not sure how to get started unit test this method below.
private static double setAfterSave(Opportunity deal) { 
        double returnValue;
        if(deal.Restaurant__c) {            
          returnValue = deal.HVAC__c == null ? 0 : deal.HVAC__c;
        } else {
           returnValue = deal.HVAC__c == null ? 0 : deal.HVAC__c;
        }
        return returnValue;
    }
Best Answer chosen by St2018
GauravendraGauravendra
Hi Stephanie,

You can try something like this.
@isTest
public static void setAfterSaveTest() {

	Opportunity opp = new Opportunity();
	opp.Restaurant__c = true;
	opp.HVAC__c = 5;
	opp.Name = 'Test opp';
	opp.StageName = 'Prospecting';
	opp.CloseDate = Date.today();
	insert opp;


	Opportunity opp1 = new Opportunity();
	opp1.Restaurant__c = false;
	opp1.HVAC__c = 5;
	opp1.Name = 'Test opp1';
	opp1.StageName = 'Prospecting';
	opp1.CloseDate = Date.today();
	insert opp1;

	ClassName.setAfterSave(opp);
	ClassName.setAfterSave(opp1);
	​​​​​​​
}
Hope this helps.

All Answers

GauravendraGauravendra
Hi Stephanie,

You can try something like this.
@isTest
public static void setAfterSaveTest() {

	Opportunity opp = new Opportunity();
	opp.Restaurant__c = true;
	opp.HVAC__c = 5;
	opp.Name = 'Test opp';
	opp.StageName = 'Prospecting';
	opp.CloseDate = Date.today();
	insert opp;


	Opportunity opp1 = new Opportunity();
	opp1.Restaurant__c = false;
	opp1.HVAC__c = 5;
	opp1.Name = 'Test opp1';
	opp1.StageName = 'Prospecting';
	opp1.CloseDate = Date.today();
	insert opp1;

	ClassName.setAfterSave(opp);
	ClassName.setAfterSave(opp1);
	​​​​​​​
}
Hope this helps.
This was selected as the best answer
Raj VakatiRaj Vakati
try this
@isTest
public static void setAfterSaveTest() {

	Opportunity opp = new Opportunity();
	opp.Restaurant__c = true;
	opp.HVAC__c = 5;
	opp.Name = 'Test opp';
	opp.StageName = 'Prospecting';
	opp.CloseDate = Date.today();
	insert opp;


	Opportunity opp1 = new Opportunity();
	opp1.Restaurant__c = false;
	opp1.HVAC__c = 5;
	opp1.Name = 'Test opp1';
	opp1.StageName = 'Prospecting';
	opp1.CloseDate = Date.today();
	insert opp1;
	
	Opportunity opp2 = new Opportunity();
	opp2.Restaurant__c = true;
	opp2.Name = 'Test opp';
	opp2.StageName = 'Prospecting';
	opp2.CloseDate = Date.today();
	insert opp2;


	Opportunity opp3 = new Opportunity();
	opp3.Restaurant__c = false;
	opp3.Name = 'Test opp1';
	opp3.StageName = 'Prospecting';
	opp3.CloseDate = Date.today();
	insert opp3;

	ClassName.setAfterSave(opp);
	ClassName.setAfterSave(opp1);
	ClassName.setAfterSave(opp2);
	ClassName.setAfterSave(opp3);
	
}

​​​​​​​
St2018St2018

@Gauravendra 
@ Raj Vakati
Im getting these two error messages.
1. Missing return statement required return type:Double
2  Method is not visible: Double  
Raj VakatiRaj Vakati
Update the method as below 
and use the same test class 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm
​​​​​​​
@TestVisible 
private static double setAfterSave(Opportunity deal) { 
        double returnValue;
        if(deal.Restaurant__c) {            
          returnValue = deal.HVAC__c == null ? 0 : deal.HVAC__c;
        } else {
           returnValue = deal.HVAC__c == null ? 0 : deal.HVAC__c;
        }
        return returnValue;
    }