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
kaushik Krishnan 15kaushik Krishnan 15 

write a test class for invocable method

Hi,
I am facing an issue for writing a test class for a invocable method which is getting called by Process Builder.
Actual class--
public class vOppDuplicate {
//public virtual class BaseException extends Exception {}
//public class OtherException extends BaseException {}
@InvocableMethod (label='Records for duplicate' description='Duplicate Check')
public static void updateOppDup(List<Id> OpportunityIds) {
try{
        List<Opportunity> oppList = [SELECT Opp_Acc_Concatenate__c,Opportunity_Unique_Name__c FROM Opportunity WHERE Id in :OpportunityIds];
        for(Opportunity opp: oppList)
         {
           opp.Opportunity_Unique_Name__c = opp.Opp_Acc_Concatenate__c;               
         } 
          update oppList;
        //return oppList;
    }
    catch(DMLException e)
    {
        //throw new OtherException('Duplicate value is present');
        throw new DMLException('Duplicate value with same opportunity and account name already present.'+'<br/>');
        //System.debug('The following exception has occurred: ' + e.getMessage());
        //if(e.getMessage().contains('Update failed')){
        //e.setMessage('Duplicate value is already present.');
        //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, e.getMessage()));
    //}

    }
    }
}

Test Class---
@isTest
private class vOppDuplicateTest {
@isTest static void testdup() {
    Account acc = new Account(name='TestClient');
    insert acc;
    Opportunity oppList = new Opportunity(Opportunity_Unique_Name__c= 'DupAccOppTest', Name = 'OppDup',AccountId=acc.Id);
    insert oppList;
    //oppList = [SELECT Id from Opportunity where Id = : oppList.Id];
    vOppDuplicate oppdup = new vOppDuplicate();
    //oppdup.updateOppDup();
}
}

How do i call the method and pass the value ?

Thanks
Best Answer chosen by kaushik Krishnan 15
{tushar-sharma}{tushar-sharma}
Simply call the method
vOppDuplicate.updateOppDup(new List<Id>{oppList.Id});

You should also use assert to validate your code is working fine.

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/

All Answers

{tushar-sharma}{tushar-sharma}
Simply call the method
vOppDuplicate.updateOppDup(new List<Id>{oppList.Id});

You should also use assert to validate your code is working fine.

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
This was selected as the best answer
kaushik Krishnan 15kaushik Krishnan 15
Thanks @Tushar. It worked and also used assert statement for validation.

Regards,
Kaushik