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
Akhil Katkam 5Akhil Katkam 5 

Test class for given code

Hi Developer Community , 

please help me with the test class for below code,
public static void fetchCompetitors(String recordId, String recId){
        List<Opportunity> opp=new List<Opportunity>();
        Opportunity o = [select id,Name,CIBoostVersion1__MainCompetitors__c from Opportunity where id=:recordId];
        List<CIBoostVersion1__Competitor__c> cmptList=[select id,Name from CIBoostVersion1__Competitor__c where id=:recId];
        for(CIBoostVersion1__Competitor__c comp : cmptList){
        o.CIBoostVersion1__MainCompetitors__c = comp.Name; 
        }
        update o; 
    }

thanks in advance
Best Answer chosen by Akhil Katkam 5
CharuDuttCharuDutt
Hii Akhil KatKam
Try Below Code
@isTest
public class fetchCompetitorsTest { 
    @isTest    
    public static void UnitTest() {
       
        Opportunity Opp= new Opportunity();
        Opp.Name = 'Test Opp';
        Opp.StageName = 'Closed Won';
        Opp.CloseDate =  system.Today();
        /*Fill Required Fields*/        
        insert Opp;
        CIBoostVersion1__Competitor__c CVC = new CIBoostVersion1__Competitor__c();
        /*Fill Required Fields*/
        insert CVC

        test.startTest();
        ClassName.fetchCompetitors(Opp.Id, CVC.Id);
        Test.stopTest();
            
    }

}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

CharuDuttCharuDutt
Hii Akhil KatKam
Try Below Code
@isTest
public class fetchCompetitorsTest { 
    @isTest    
    public static void UnitTest() {
       
        Opportunity Opp= new Opportunity();
        Opp.Name = 'Test Opp';
        Opp.StageName = 'Closed Won';
        Opp.CloseDate =  system.Today();
        /*Fill Required Fields*/        
        insert Opp;
        CIBoostVersion1__Competitor__c CVC = new CIBoostVersion1__Competitor__c();
        /*Fill Required Fields*/
        insert CVC

        test.startTest();
        ClassName.fetchCompetitors(Opp.Id, CVC.Id);
        Test.stopTest();
            
    }

}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer
Akhil Katkam 5Akhil Katkam 5
Thanks CharuDutt