Activity Forums Salesforce® Discussions How to try and catch block is covered in Salesforce Testing?

  • How to try and catch block is covered in Salesforce Testing?

    Posted by Aditya on February 14, 2020 at 12:54 pm

    How to try and catch block is covered in Salesforce Testing?

    MOHIT replied 5 years ago 2 Members · 1 Reply
  • 1 Reply
  • MOHIT

    Member
    February 18, 2020 at 11:33 am

    There is no way to get Exeption in below code

    String shref = ApexPages.currentPage().getURL();

    That is why exception part is covering. If you will pass null value in shref then also excpetion will not come till you will not use shref variable.

    If you want to cover catch in any cost try to update your code like below

    public with sharing class HouseholdMemberController {

    public HouseholdMemberController() {

    System.debug('>>> HouseholdMemberController BEFORE shref');
    try {
    String shref = ApexPages.currentPage().getURL();
    System.debug('>>> HouseholdMemberController shref='+shref);
    if(Test.isRunningTest())
    {
    throw new Exception();
    }

    } catch (Exception e) {
    System.debug('>>> ERROR ='+e);
    }
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos