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
Saurabh Bisht 19Saurabh Bisht 19 

can we make bulk test Apex class for this

@isTest
public class StateNameTest{
   
    @testSetup static void setup()
    {
        StateName__c newStates=new StateName__c();
        newStates.Name='Goa';
        insert newStates;
       
    }

    @isTest
    static void checkStatenamesTest()
    {
        StateName__c testStates=[Select Id from StateName__c where State__c='Goa'];
        Opportunity newOpportunity=new Opportunity();
        newOpportunity.Name='Saurabh Bisht';
        newOpportunity.CloseDate=System.today().addMonths(1);
        newOpportunity.StageName='Qualification';
        newOpportunity.State__c='Goa';
        insert newOpportunity;
        Opportunity testOpportunity=[Select Id,StateName__c from Opportunity where id=:newOpportunity.Id];
        system.assertequals(testStates.Id,testOpportunity.StateName__c);
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Saurabh,

What is the issue, you were facing?

Thanks!!

 
Saurabh Bisht 19Saurabh Bisht 19
i didnt get any problem this test fully pass 100% test coverage
if you watch it i create a single record odf statename as Goa and
one opportunity in second metyhod so iwant this class should run with 
bulk records bulk records od statename custom obj and opportunity bulk records.
can you make same test class for bulk
Saurabh Bisht 19Saurabh Bisht 19
this my class helper:
public class StateNameHelper{
    public static Map<String,Id> getRecords(List<Opportunity> listOfOpportunities)
    {
        Set<String> setOfStates=new Set<String>();
        for(Opportunity op:listOfOpportunities)
        {
            if(op.State__c!=null)
            {
                setOfStates.add(op.State__c);
            }
        }
        Map<String,Id> mapOfStates=new Map<String,Id>();
        List<StateName__c> stateNameList=[Select Id,Name from StateName__c where Name=:setOfStates];
        for(StateName__c st:stateNameList)
        {
            mapOfStates.put(st.Name,st.Id);
        }
        return mapOfStates;
    }
    public static void checkStatenames(Map<String,Id> statesMaps,Opportunity ops)
    {
        if(statesMaps.containsKey(ops.State__c) && ops.State__c!=null)
        {
            ops.StateName__c=statesMaps.get(ops.State__c);
        }
       else if(!statesMaps.containsKey(ops.State__c) && ops.State__c!=null)
        {
            ops.addError('wE need to create account');
        }
    }
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Saurabh,

try with below code.
@isTest
public class StateNameTest{
   
    @testSetup static void setup()
    {
	
	  List<StateName__c> liststates = new List<StateName__c>();
	  
	  For(Integer i=1 ;i<=10 ; i++){
	  string str1 = 'Goa';
	  string str2 = 'Bangalore';
	  
	  string state = i<5? str1:str2;
	    StateName__c newStates=new StateName__c();
        newStates.Name=state;
		liststates.add(newStates);
	  }

        insert liststates;
       
    }

    @isTest
    static void checkStatenamesTest()
    {
        StateName__c testStates=[Select Id from StateName__c where State__c='Goa'];
        Opportunity newOpportunity=new Opportunity();
        newOpportunity.Name='Saurabh Bisht';
        newOpportunity.CloseDate=System.today().addMonths(1);
        newOpportunity.StageName='Qualification';
        newOpportunity.State__c='Goa';
        insert newOpportunity;
        Opportunity testOpportunity=[Select Id,StateName__c from Opportunity where id=:newOpportunity.Id];
        system.assertequals(testStates.Id,testOpportunity.StateName__c);
}

If this helps, Please mark it as best answer.

Thanks!!