+ Start a Discussion
Rohini Chaudhary 14Rohini Chaudhary 14 

Hii All ,can anyone help me in increasing the code coverage of this trigger handler test class to 100%?

Apex Class:
public with sharing class ChangeOpptyOwnerCtrl {
    private string oppId;
    public Opportunity oppobj {get;set;}
    public boolean isErrInSave {get;set;}
    
    public ChangeOpptyOwnerCtrl(ApexPages.StandardController ctrl){
        oppId = ApexPages.currentPage().getParameters().get('oppId');
        if(oppId != null)
            oppobj = [Select id, Name, OwnerId from Opportunity where id =: oppId limit 1];
    }
    
    public void saveOwner(){
        isErrInSave = false;
        try{
            if(oppobj != null)
                update oppobj;
        }catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error , e.getMessage()));
            isErrInSave = true;
        }
    }
}


TestClass:

@isTest
public with sharing class ChangeOpptyOwnerCtrlTest {
    @testSetup
    static void setupTestData(){
        Account acc = TestUtility.createAccount('Test A');
        insert acc;
        Opportunity opp = TestUtility.createOpportunity('@test opp', Date.today(), 'To be Invoiced', acc.id);
        opp.Follow_up_Date__c = date.today();
        insert opp;
    }
    
    testmethod static void saveOwnerTest(){
        Opportunity opp = [Select id from Opportunity limit 1];
        test.startTest();
        
        Test.setCurrentPageReference(new PageReference('Page.ChangeOpptyOwnerPage'));
        System.currentPageReference().getParameters().put('oppId',opp.id);        
        ApexPages.StandardController sc = new ApexPages.StandardController(opp);
        ChangeOpptyOwnerCtrl ctrlObj = new ChangeOpptyOwnerCtrl(sc);
        
        ctrlObj.saveOwner();
        
        test.stopTest();
        
    }
    
}



can any one help me to cover catch(Exception e) lines
Best Answer chosen by Rohini Chaudhary 14
Maharajan CMaharajan C
Rohini, Please mark the best Answer to close this thread...

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Rohini,

Refer the below link have solution and modify the test data as per your code.

https://developer.salesforce.com/forums/?id=9060G000000MRo2QAG

If this helps, please mark it as best answer.

thanks!!
Maharajan CMaharajan C
Hi Rohini,

I think you are not able to cover the catch blog...So, you have to pass opp data which will fail in update due to some validation rule or trigger then you can cover the catch.  Also, you can use the below trick to cover the catch block ...
 
@isTest
public with sharing class ChangeOpptyOwnerCtrlTest {
    @testSetup
    static void setupTestData(){
        Account acc = TestUtility.createAccount('Test A');
        insert acc;
        Opportunity opp = TestUtility.createOpportunity('@test opp', Date.today(), 'To be Invoiced', acc.id);
        opp.Follow_up_Date__c = date.today();
        insert opp;        
    }
    
    testmethod static void saveOwnerTest(){
        Opportunity opp = [Select id from Opportunity limit 1];
        test.startTest();
        
        Test.setCurrentPageReference(new PageReference('Page.ChangeOpptyOwnerPage'));
        System.currentPageReference().getParameters().put('oppId',opp.id);        
        ApexPages.StandardController sc = new ApexPages.StandardController(opp);
        ChangeOpptyOwnerCtrl ctrlObj = new ChangeOpptyOwnerCtrl(sc);
        
        ctrlObj.saveOwner();
        
        test.stopTest();
        
    }
    
    testmethod static void saveOwnerExceptionTest(){
        Opportunity opp = new Opportunity(Name = 'Test Opp');
        test.startTest();
        
        Test.setCurrentPageReference(new PageReference('Page.ChangeOpptyOwnerPage'));
        ApexPages.StandardController sc = new ApexPages.StandardController(opp);
        ChangeOpptyOwnerCtrl ctrlObj = new ChangeOpptyOwnerCtrl(sc);
        ctrlObj.oppobj = opp;
        
        ctrlObj.saveOwner();
        
        test.stopTest();
        
    }
    
}

Thanks,
Maharajan.C
Rohini Chaudhary 14Rohini Chaudhary 14
It worked.
Maharajan CMaharajan C
Rohini, Please mark the best Answer to close this thread...
This was selected as the best answer
Rohini Chaudhary 14Rohini Chaudhary 14
can you also help for the system.assertion???/
Shalini RaoShalini Rao
Hello, Maharajan!

Thank you for providing the solution. I will work on this to fix the error.

Thank you!

Here is our latest post, please have a look at the best Google Chrome extensions (https://www.allnewtechbuzz.info/best-google-chrome-extensions-for-seo/).
Dheeraj JangraDheeraj Jangra
Keep up the good work, Thank you for providing the solution. I will work on this to fix the error. great info.
Feel free to visit my web page Minecraft apk mod free download check this site:-https://theminecrafts.com/
Dheeraj JangraDheeraj Jangra
Keep up the good work, Thank you for providing the solution. I will work on this to fix the error. great info.
Feel free to visit my web page Minecraft apk mod here (https://theminecrafts.com/). 
senies12 joesenies12 joe
Great Post! Thank you for visiting here. mysainsburys (https://tractorsinfo.com/mysainsburys/)