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
Vidya H 4Vidya H 4 

hi please helpp me to write test class for this batch class

global class UpdateFunding implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT Id,StageName FROM opportunity where StageName='Closed Won']);
    }

    global void execute(Database.BatchableContext context, List<sObject> batch){
        Set<Id> Opportuniyids = new Set<Id>();

        for (sObject oppy : batch) {
            Opportuniyids.add(oppy.Id);
        }

        updatequote(Opportuniyids);
    }

    global void finish(Database.BatchableContext context) {}

    public void updatequote(set<id> oppyids){
        system.debug('opportuiy ids'+oppyids);
        List<opportunity> opportunities=[SELECT Id ,Funding__c FROM opportunity WHERE Id IN :oppyids];
        for(Opportunity opp:opportunities) {
       Set<String> systems = new Set<String>();
        for (Quote__c oppObj : [SELECT Id, Funding__c, opportunity__c FROM Quote__c WHERE opportunity__c = :opp.id]) {
        if (String.isNotBlank(oppObj.Funding__c)) {
            systems.addAll(oppObj.Funding__c.split(';'));
        }
    }
    String accountSystems = '';
    for (String value : systems) {
        accountSystems += value + ';';
    }
    accountSystems = accountSystems.removeEnd(';');
    try {
        update new Opportunity(Id = opp.id,Funding__c  = accountSystems);
    } catch (Exception e) {
        System.debug('Exception: ' + e.getMessage());
    }
        }       
    }

}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below test class.
 
@istest
public class UpdateFundingTest {

    @isTest
    public static void clienteTriggerTest() {
        
Account acc = new Account(
Name = 'SFDC Account',
Rating = 'Hot',
Industry = 'Banking'
);
insert acc;

// Create Opportunity
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.Name = 'Test Opportunity';
opp.CloseDate= System.Today();
opp.StageName='Closed Won';
opp.Pricebook2Id = Test.getStandardPricebookId();
insert opp;
        Quote__c q = new Quote__c();
        q.Opportunity__c=opp.id;
        q.Funding__c='Partial';
        insert q;
        Quote__c q1 = new Quote__c();
        q1.Opportunity__c=opp.id;
        q1.Funding__c='Full';
        insert q1;
        UpdateFunding u= new UpdateFunding();
        database.executeBatch(u);
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
CharuDuttCharuDutt
Hii Vidya 
Try Below Test Class
@istest
public class UpdateFundingTest {
    @isTest
    public static void clienteTriggerTest() {
        
        Account acc = new Account(
        Name = 'Test Account',
        Rating = 'Hot',
        Industry = 'Agriculture'
        );
        insert acc;

        Opportunity opp = new Opportunity();
        opp.AccountId = acc.Id;
        opp.Name = 'Test Opportunity';
        opp.CloseDate= System.Today();
        opp.StageName='Closed Won';
        opp.Pricebook2Id = Test.getStandardPricebookId();
        insert opp;

        Quote__c q = new Quote__c();
        q.Opportunity__c=opp.id;
        q.Funding__c='Partial';
        insert q;

        Quote__c q1 = new Quote__c();
        q1.Opportunity__c=opp.id;
        q1.Funding__c='Full';
        insert q1;

        UpdateFunding uf= new UpdateFunding();
        database.executeBatch(uf);
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You! 
Vidya H 4Vidya H 4

@CharuDutt 
need to test for negetive scenario also