+ Start a Discussion
Markey1Markey1 

How to test Catch Blocks

Anyone know how to test Catch blocks. I've looked around for creating Catch test conditions but have been unsuccessful so far.

 

Code Snippet:

            insert skEnroll;
        } 
        catch (DMLException e) {            
            ApexPages.addMessages(e);            
            return null;
        }
        return page.aeskenrollconfirm; 
    }        
}

 

dnakonidnakoni

What you could do in your test class is try to force the controller to insert an invalid record. E.g. if your try block has "insert skEnroll", and you have control over what fields are populated before skEnroll is inserted, try omitting a required field, .e.g. Name. That should throw an error and trigger the catch block.

 

 

Markey1Markey1

Thanks for the reply. I'm using the SFDC UI (not Eclipse or Force.com IDE) and when I try to force an error, it is caught by the UI and actually reduces my coverage. I highlighted the rows that are causing issues in the class and you will see this is not the only peice lacking coverage . Any help you can provide with the below test class is much appreciated.

  

Class AESK Test:

public with sharing class AESK_Test {
 
    //Variables
    public SafeKey_Certification__c skTest;
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}
    public String test_us001pareq {get; set;}
    public String test_us001pares {get; set;}    
    public String test_us002vereq {get; set;}
    public String test_us002veres {get; set;}  
    public String test_us002pareq {get; set;}
    public String test_us002pares {get; set;}    
    public String test_us003vereq {get; set;}
    public String test_us003veres {get; set;} 
    public String test_us003pareq {get; set;}
    public String test_us003pares {get; set;}     
    public String test_us005vereq {get; set;}
    public String test_us005veres {get; set;}  
    public String test_us005pareq {get; set;}
    public String test_us005pares {get; set;}   
    
    public String test_tc01crreq {get; set;} 
    public String test_tc01crres {get; set;}  
    public String test_tc02vereq {get; set;} 
    public String test_tc02veres {get; set;} 
    public String test_tc02pareq {get; set;} 
    public String test_tc02pares {get; set;} 
    public String test_tc04vereq {get; set;} 
    public String test_tc04veres {get; set;} 
    public String test_tc04pareq {get; set;} 
    public String test_tc04pares {get; set;}    
    public String test_tc05avereq {get; set;} 
    public String test_tc05averes {get; set;} 
    public String test_tc05apareq {get; set;} 
    public String test_tc05apares {get; set;}   
    public String test_tc08vereq {get; set;} 
    public String test_tc08veres {get; set;} 
    public String test_tc08pareq {get; set;} 
    public String test_tc08pares {get; set;}   
    public String test_tc09vereq {get; set;} 
    public String test_tc09veres {get; set;} 
    public String test_tc09pareq {get; set;} 
    public String test_tc09pares {get; set;} 
    public String test_tc11vereq {get; set;} 
    public String test_tc11veres {get; set;} 
    public String test_tc11pareq {get; set;} 
    public String test_tc11pares {get; set;}   
    public String test_tc13vereq {get; set;} 
    public String test_tc13veres {get; set;} 
    public String test_tc13pareq {get; set;} 
    public String test_tc13pares {get; set;}  
    public String test_tc15vereq {get; set;} 
    public String test_tc15veres {get; set;} 
    public String test_tc15pareq {get; set;} 
    public String test_tc15pares {get; set;}                            
                          
    //Controller
    public AESK_Test(ApexPages.StandardController con){
        skTest = (SafeKey_Certification__c)con.getRecord();
    }  
    
    //Attachment
    public Attachment attachment {
    get {
        if (attachment == null)
          attachment = new Attachment();
        return attachment;
    }
    set;
    }              

    //Validate, save, and return page
    public PageReference save() {                                                                                                         
        try {        
            if(skTest.Submission_Type__c == 'New Submission' && (attachment.name == '' || attachment.description == '' || attachment.body == null)) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'All upload fields are required.'));            
            return null;
            }
                  
            String pagert = ApexPages.currentPage().getParameters().get('RecordTypeId');
            /*Issuer*/
            if(pagert == '012500000001LI') {
                skTest.RecordTypeId = '012500000001LI';        
            }
            /*Merchant*/
            else if(pagert == '012500000001LIn') {
                skTest.RecordTypeId = '012500000001LIn';        
            }     
            /*ACS Vendor*/             
            else if(pagert == '012500000001LIs') {
                skTest.RecordTypeId = '012500000001LIs';        
            }
            /*MPI Vendor*/        
            else if(pagert == '012500000001LJ2') {
                skTest.RecordTypeId = '012500000001LJ2';        
            }             
            else {
                return null;
            } 
      
            //set field values
            skTest.US001__c = 'Start: US001VEReq' + '\n' + test_us001vereq + '\n' + 'Stop: US001VEReq' + '\n\n' + 'Start: US001VERes' + '\n' + test_us001veres + '\n' + 'Stop: US001VERes' + '\n\n' +
                                'Start: US001PAReq' + '\n' + test_us001pareq + '\n' + 'Stop: US001PAReq' + '\n\n' + 'Start: US001PARes' + '\n' + test_us001pares + '\n' + 'Stop: US001PARes';
                        
            skTest.US002__c = 'Start: US002VEReq' + '\n' + test_us002vereq + '\n' + 'Stop: US002VEReq' + '\n\n' + 'Start: US002VERes' + '\n' + test_us002veres + '\n' + 'Stop: US002VERes' + '\n\n' +
                                'Start: US002PAReq' + '\n' + test_us002pareq + '\n' + 'Stop: US002PAReq' + '\n\n' + 'Start: US002PARes' + '\n' + test_us002pares + '\n' + 'Stop: US002PARes';
            
            skTest.US003__c = 'Start: US003VEReq' + '\n' + test_us003vereq + '\n' + 'Stop: US003VEReq' + '\n\n' + 'Start: US003VERes' + '\n' + test_us003veres + '\n' + 'Stop: US003VERes' + '\n\n' +
                                'Start: US003PAReq' + '\n' + test_us003pareq + '\n' + 'Stop: US003PAReq' + '\n\n' + 'Start: US003PARes' + '\n' + test_us003pares + '\n' + 'Stop: US003PARes';
            
            skTest.US005__c = 'Start: US005VEReq' + '\n' + test_us005vereq + '\n' + 'Stop: US005VEReq' + '\n\n' + 'Start: US005VERes' + '\n' + test_us005veres + '\n' + 'Stop: US005VERes' + '\n\n' + 
                                'Start: US005PAReq' + '\n' + test_us005pareq + '\n' + 'Stop: US005PAReq' + '\n\n' + 'Start: US005PARes' + '\n' + test_us005pares + '\n' + 'Stop: US005PARes';                        

            skTest.TC01__c = 'Start: TC01CRReq' + '\n' + test_tc01crreq + '\n' + 'Stop: TC01CRReq' + '\n\n' + 'Start: TC01CRRes' + '\n' + test_tc01crres + '\n' + 'Stop: TC01CRRes';

            skTest.TC02__c = 'Start: TC02VEReq' + '\n' + test_tc02vereq + '\n' + 'Stop: TC02VEReq' + '\n\n' + 'Start: TC02VERes' + '\n' + test_tc02veres + '\n' + 'Stop: TC02VERes' + '\n\n' + 
                                'Start: TC02PAReq' + '\n' + test_tc02pareq + '\n' + 'Stop: TC02PAReq' + '\n\n' + 'Start: TC02PARes' + '\n' + test_tc02pares + '\n' + 'Stop: TC02PARes';                        
                                  
            skTest.TC04__c = 'Start: TC04VEReq' + '\n' + test_tc04vereq + '\n' + 'Stop: TC04VEReq' + '\n\n' + 'Start: TC04VERes' + '\n' + test_tc04veres + '\n' + 'Stop: TC04VERes' + '\n\n' + 
                                'Start: TC04PAReq' + '\n' + test_tc04pareq + '\n' + 'Stop: TC04PAReq' + '\n\n' + 'Start: TC04PARes' + '\n' + test_tc04pares + '\n' + 'Stop: TC04PARes';    

            skTest.TC05A__c = 'Start: TC05AVEReq' + '\n' + test_tc05avereq + '\n' + 'Stop: TC05AVEReq' + '\n\n' + 'Start: TC05AVERes' + '\n' + test_tc05averes + '\n' + 'Stop: TC05AVERes' + '\n\n' + 
                                'Start: TC05APAReq' + '\n' + test_tc05apareq + '\n' + 'Stop: TC05APAReq' + '\n\n' + 'Start: TC05APARes' + '\n' + test_tc05apares + '\n' + 'Stop: TC05APARes';                

            skTest.TC08__c = 'Start: TC08VEReq' + '\n' + test_tc08vereq + '\n' + 'Stop: TC08VEReq' + '\n\n' + 'Start: TC08VERes' + '\n' + test_tc08veres + '\n' + 'Stop: TC08VERes' + '\n\n' + 
                                'Start: TC08PAReq' + '\n' + test_tc08pareq + '\n' + 'Stop: TC08PAReq' + '\n\n' + 'Start: TC08PARes' + '\n' + test_tc08pares + '\n' + 'Stop: TC08PARes';    

            skTest.TC09__c = 'Start: TC09VEReq' + '\n' + test_tc09vereq + '\n' + 'Stop: TC09VEReq' + '\n\n' + 'Start: TC09VERes' + '\n' + test_tc09veres + '\n' + 'Stop: TC09VERes' + '\n\n' + 
                                'Start: TC09PAReq' + '\n' + test_tc09pareq + '\n' + 'Stop: TC09PAReq' + '\n\n' + 'Start: TC09PARes' + '\n' + test_tc09pares + '\n' + 'Stop: TC09PARes';    
                                
            skTest.TC11__c = 'Start: TC11VEReq' + '\n' + test_tc11vereq + '\n' + 'Stop: TC11VEReq' + '\n\n' + 'Start: TC11VERes' + '\n' + test_tc11veres + '\n' + 'Stop: TC11VERes' + '\n\n' + 
                                'Start: TC11PAReq' + '\n' + test_tc11pareq + '\n' + 'Stop: TC11PAReq' + '\n\n' + 'Start: TC11PARes' + '\n' + test_tc11pares + '\n' + 'Stop: TC11PARes';                                                

            skTest.TC13__c = 'Start: TC13VEReq' + '\n' + test_tc13vereq + '\n' + 'Stop: TC13VEReq' + '\n\n' + 'Start: TC13VERes' + '\n' + test_tc13veres + '\n' + 'Stop: TC13VERes' + '\n\n' + 
                                'Start: TC13PAReq' + '\n' + test_tc13pareq + '\n' + 'Stop: TC13PAReq' + '\n\n' + 'Start: TC13PARes' + '\n' + test_tc13pares + '\n' + 'Stop: TC13PARes';     

            skTest.TC15__c = 'Start: TC15VEReq' + '\n' + test_tc15vereq + '\n' + 'Stop: TC15VEReq' + '\n\n' + 'Start: TC15VERes' + '\n' + test_tc15veres + '\n' + 'Stop: TC15VERes' + '\n\n' + 
                                'Start: TC15PAReq' + '\n' + test_tc15pareq + '\n' + 'Stop: TC15PAReq' + '\n\n' + 'Start: TC15PARes' + '\n' + test_tc15pares + '\n' + 'Stop: TC15PARes';         
                       
            insert skTest;     
                                                                               
            //attachment
            attachment.ParentId = skTest.Id; //the record the file is attached to
            attachment.OwnerId = UserInfo.getUserId();
            attachment.IsPrivate = false; 
            
            if(attachment.name != '' && attachment.description != '' && attachment.body != null) {
                insert attachment;
            }
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        finally {
            attachment = new Attachment();
        }
    return page.aesktestconfirm; 
    } 
}

 Test Class:

public class testSafeKey {
    
    static testMethod void testTest() { 
        //Setup Page Reference
        PageReference testPref = Page.aesksetest;
        Test.setCurrentPage(testPref);
                                
        //Setup Controller
        ApexPages.StandardController testCon = new ApexPages.StandardController(new SafeKey_Certification__c());
        AESK_Test testExt = new AESK_Test(testCon);
            
        //Setup Record Types
        RecordType iss = [SELECT id FROM RecordType WHERE  SobjectType = 'SafeKey_Certification__c' AND Name = 'Issuer'];

        //Setup Attachments
        testExt.attachment.OwnerId = UserInfo.getUserId();
        testExt.attachment.IsPrivate = false;
        testExt.attachment.name = 'AttachmentName';
        testExt.attachment.description = 'AttachmentDescription';
        testExt.attachment.body = Blob.valueOf('AESK_Enroll.txt');                                   

        //Test Issuer Certification
        SafeKey_Certification__c certification = new SafeKey_Certification__c(
            RecordTypeId = iss.Id,
            SKID__c = 'AESK8008',
            Submission_Type__c = 'Re-Submission');                        
                                                                                 
        PageReference result = testExt.save();    
        insert certification;   
                                                                                     
    }    
}

 

VKrishVKrish

I am facing same problem. I am developing in Salesforce UI. I want to cover a sample catch block for DML Exception.

I have 70% coverage which excludes only the statements under catch block.

When I use -ve use case in test method it throws me error and fails the test resulting 0% covering.

When I use try catch within test method (using asserts to expect DML exception the test class runs - but still leaves me with 70% coverage excluding the catch statements in original trigger.

Can anyone of you let me know how did you solve your porblem?

 

Thanks in advance!