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
TestsalesforceAccountTestsalesforceAccount 

test class requires

can you please anyone help me how to write test class for below code.

public class UpdateOpptyController {

    @AuraEnabled
    public static void updateOpportunity(Opportunity oppty){
        UPDATE oppty;
    }
    
      @AuraEnabled
    public static Opportunity fetchOpportunityDetails(Id oppId) {
        List<Opportunity> lstOfStd = new List<Opportunity>();
        system.debug('=='+oppId);
        if(oppId != null) {
            lstOfStd = [SELECT Id, Name,checkbox1__c,Checkbox2__c,Checkbox3__c FROM Opportunity WHERE Id = :oppId];
            system.debug('=='+lstOfStd);
        }   
         
        if(!lstOfStd.isEmpty()){
            return lstOfStd.get(0);
        }
         
        
        return null;
    }
}
Best Answer chosen by TestsalesforceAccount
Sampath SuranjiSampath Suranji
Hi,

Check below code
@isTest
public class updateOpportunityTest {

    public static testmethod void fetchOpportunityDetailsTest(){
        test.startTest();
        Opportunity objOpp= new Opportunity(name='new Opp', stageName='Cclosed won', closeDate=System.Date.today());
        insert objOpp;
        Opportunity opp= UpdateOpptyController.fetchOpportunityDetails(objOpp.Id);
        Opportunity oppNegative= UpdateOpptyController.fetchOpportunityDetails(null);
        UpdateOpptyController.updateOpportunity(objOpp);
        test.stopTest();
    }
    
}

regards
 

All Answers

Sampath SuranjiSampath Suranji
Hi,

Check below code
@isTest
public class updateOpportunityTest {

    public static testmethod void fetchOpportunityDetailsTest(){
        test.startTest();
        Opportunity objOpp= new Opportunity(name='new Opp', stageName='Cclosed won', closeDate=System.Date.today());
        insert objOpp;
        Opportunity opp= UpdateOpptyController.fetchOpportunityDetails(objOpp.Id);
        Opportunity oppNegative= UpdateOpptyController.fetchOpportunityDetails(null);
        UpdateOpptyController.updateOpportunity(objOpp);
        test.stopTest();
    }
    
}

regards
 
This was selected as the best answer
Raj VakatiRaj Vakati
Try this code
 
@isTest
public class updateOpportunityTest {

    public static testmethod void fetchOpportunityDetailsTest(){
        Test.startTest();
		// Add any other required fields 
        Opportunity objOpp= new Opportunity(Name='new Opp', StageName ='closed won', CloseDate=System.Date.today(),Amount=120);
        insert objOpp;
        Opportunity opp= UpdateOpptyController.updateOpportunity(objOpp);
        Opportunity oppNegative= UpdateOpptyController.fetchOpportunityDetails(objOpp.Id);
        System.assert('new Opp',oppNegative.Name) ;
        Test.stopTest();
    }
    
}

 
TestsalesforceAccountTestsalesforceAccount
HI 
Sampath Suranji,

Thank you so much. its covered 100%.

Regards,
Nagaraj