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
Yelper DevYelper Dev 

TestDataFactory and OpportunityHandlerTest How to?

Hello,

Would like to ask on how to create a testdatafactory and test class for the following code below?

Trigger Handler:
public class OpportunityTriggerHandler {
    public static void onBeforeUpdate(List<Opportunity> oppList, Map<Id, Opportunity> oldStageMap){
        oppNewStage(oppList, oldStageMap);
    }
	
    public static void oppNewStage(List<Opportunity> oppList, Map<Id, Opportunity> oldStageMap){
        for(Opportunity opp : oppList){
            //Compare the old stage status and new stage status.
            Opportunity oldOppID = oldStageMap.get(opp.Id);           
                if(oldOppID.StageName !=opp.StageName){
                    if(opp.StageName == 'Closed Won'){
                        opp.Description = 'This Opportunity is Closed Won.';
                    }                    
                }           
        }
    }
}
And Trigger is:
trigger OpportunityTrigger on Opportunity (before update) {
    if(Trigger.isBefore)
    {        
        if(Trigger.isUpdate){
            OpportunityTriggerHandler.onBeforeUpdate(Trigger.new, Trigger.oldMap);
        }
    }
}

Thank you!
 
Best Answer chosen by Yelper Dev
Martha VMartha V
try this -- and check that you are giving value to all opportunity required fields
 
IF(NOT(ISNULL( Business_Approver_Limit_Override__c)), 
    Business_Approver_Limit_Override__c , 
@isTest
public class TestDataFactory {

    public static Id createAccount(String name){
        Account account = new Account(Name = name);
        insert account;
        return account.id;
    }

    public static Opportunity createOppData(Integer oppCounter, String stageName, id accId){
        Opportunity opp = new Opportunity();
        // check what other fields are required for the Opportunity object to be saved
        opp.AccountId = accId;
        opp.Name = 'Test Opportunity' + '' + oppCounter;
        opp.CloseDate = System.today().addMonths(1);
        opp.StageName = stageName;
        insert opp;
    }
}



@isTest
public class OpportunityTriggerHandlerTest {
    @testSetup static void createOppTestData(){
        //Create test opportunity status records
        id accId = TestDataFactory.createAccount('test account');
        Opportunity oppTempData = TestDataFactory.createOppData(0, 'Value Proposition', accId);
            insert 	oppTempData;
    }
    
    @isTest static void testOppNewStage(){
        Opportunity oppty = [SELECT Id FROM Opportunity WHERE Name = 'Test Opportunity 0'];
        oppty.StageName = 'Closed Won';      
       
        Test.startTest();
        update oppty;
        Test.stopTest();   
        
        Opportunity updateDesc = [SELECT Id FROM Opportunity WHERE Name = 'Test Opportunity 0' ];
        
        System.assertEquals(updateDesc.Description, 'This Opportunity is Closed Won.');
    }
}



 

All Answers

Martha VMartha V
The TestDataFactory is a class that will add records to use in the test. The reason to have this class is so that you don't have to write the same code over and over for each test you are performing in your org. In your case, your TestDataFactory will include methods for adding Account records (which you will need in order to add an opportunity record).

On your test method you would call the TestDataFactory and create one or more Account records, then query the account object to get the id's of the records created. With than info you can then add Opportunity records that will trigger your trigger class. Afterwards you can query the opportunity object to see if you get the expected results.
Yelper DevYelper Dev
Hi Martha, done doing the testdatafactory please see code below. 
@isTest
public class TestDataFactory {
    public static Opportunity createOppData(Integer oppCounter, String stageName){
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity' + '' + oppCounter;
        opp.CloseDate = System.today().addMonths(1);
        opp.StageName = stageName;
        return opp;
    }
}


But i'm stuck with my Test Class, any idea?
 
@isTest
public class OpportunityTriggerHandlerTest {
    @testSetup static void createOppTestData(){
        //Create test opportunity status records
        Opportunity oppTempData = TestDataFactory.createOppData(0, 'Value Proposition');
            insert 	oppTempData;
    }
    
    @isTest static void testOppNewStage(){
        Opportunity oppty = [SELECT Id FROM Opportunity WHERE Name = 'Test Opportunity 0'];
        oppty.StageName = 'Closed Won';      
       
        Test.startTest();
        update oppty;
        Test.stopTest();   
        
        Opportunity updateDesc = [SELECT Id FROM Opportunity WHERE Name = 'Test Opportunity 0' ];
        
        System.assertEquals(updateDesc.Description, 'This Opportunity is Closed Won.');
    }
}

 
Martha VMartha V
To insert an opportunity your I must have a parent record (Account) so in your test you need to add one or more Account records. Then you need to have a value for all required fields in the opportunity record not only for the Name and the StageName. So I would add another static method to your test data factory that inserts Accounts. Of course you could add that functionality to the same method you already have, but if you separate them you would have a more general use of it for future test classes. Sorry I can’t give you more help now, but I am replying to this on my phone. I will check later when I can use my computer and see if you still need help. Martha C. Vance
Martha VMartha V
try this -- and check that you are giving value to all opportunity required fields
 
IF(NOT(ISNULL( Business_Approver_Limit_Override__c)), 
    Business_Approver_Limit_Override__c , 
@isTest
public class TestDataFactory {

    public static Id createAccount(String name){
        Account account = new Account(Name = name);
        insert account;
        return account.id;
    }

    public static Opportunity createOppData(Integer oppCounter, String stageName, id accId){
        Opportunity opp = new Opportunity();
        // check what other fields are required for the Opportunity object to be saved
        opp.AccountId = accId;
        opp.Name = 'Test Opportunity' + '' + oppCounter;
        opp.CloseDate = System.today().addMonths(1);
        opp.StageName = stageName;
        insert opp;
    }
}



@isTest
public class OpportunityTriggerHandlerTest {
    @testSetup static void createOppTestData(){
        //Create test opportunity status records
        id accId = TestDataFactory.createAccount('test account');
        Opportunity oppTempData = TestDataFactory.createOppData(0, 'Value Proposition', accId);
            insert 	oppTempData;
    }
    
    @isTest static void testOppNewStage(){
        Opportunity oppty = [SELECT Id FROM Opportunity WHERE Name = 'Test Opportunity 0'];
        oppty.StageName = 'Closed Won';      
       
        Test.startTest();
        update oppty;
        Test.stopTest();   
        
        Opportunity updateDesc = [SELECT Id FROM Opportunity WHERE Name = 'Test Opportunity 0' ];
        
        System.assertEquals(updateDesc.Description, 'This Opportunity is Closed Won.');
    }
}



 
This was selected as the best answer
Yelper DevYelper Dev
It is now working thanks Martha! I forgot to include Description in the query of updateDesc