0

How can I get additional code coverage? I'm getting a de reference to null pointer that is preventing the conditional branch from being executed in the test.

enter image description here

My supporting unit test:

public static Id getRecordTypeId(SObjectType objectType, string recordTypeName) {
Id recordTypeId = null;

Map<string, RecordTypeInfo> recordTypes = objectType.getDescribe().getRecordTypeInfosByName();
if (recordTypes.containsKey(recordTypeName)) {
    recordTypeId = recordTypes.get(recordTypeName).getRecordTypeId();
}
return recordTypeId;
}

public static Account_Summary__c getAccountSummary(Id accountId) {
    Account_Summary__c accountSummary = new Account_Summary__c();
    accountSummary.Account_Name__c = accountId;
    accountSummary.Name = String.valueOf(System.today().year());
    accountSummary.Program_Name__c = 'Test Account Summary Program ' + accountSummary.Name;
    accountSummary.Report_Status__c = 'Draft';
return accountSummary;
}

My controller unit test:

@IsTest static void testredirectCREPage() {

    //Boolean IsNew = false;
    Id recordTypeId = UnitTest.getRecordTypeId(Account_Summary__c.SObjectType, 'New Account Summary');    
    System.assertNotEquals(null, recordTypeId);

    Account testAccount = UnitTest.getAccount();
    testAccount.External_ID_BA__c = '99999';
    insert testAccount;

    Account_Summary__c accountSummary = UnitTest.getAccountSummary(testAccount.Id);        
    accountSummary.RecordTypeId = recordTypeId; 

    insert accountSummary;  

    accountSummary = queryAccountSummary(accountSummary.Id);

    PageReference testPage = Page.PROD_UW_AccountSummary;
    testPage.getParameters().put('id', accountSummary.Id);
    Test.setCurrentPage(testPage);

    ApexPages.StandardController standardController = new ApexPages.StandardController(new Account_Summary__c());

    AccountSummaryController controller = new AccountSummaryController(standardController);
    controller.IsNew = false;

    Test.startTest();        
    PageReference redirectCRE = controller.redirectCREPage();
    Test.stopTest();
}
0
1

Your unit test is passing in a new record:

ApexPages.StandardController standardController = new ApexPages.StandardController(new Account_Summary__c());

When instead you should be passing in the existing record:

ApexPages.StandardController standardController = new ApexPages.StandardController(accountSummary);

You should get rid of the try-catch block, and ditch all the system.debug statements, as well as getting rid of the unused variables in your code.

You're going to also need to use a second unit test to cover the situation when the record type is not "New Account Summary".

Your code is presumably failing because if(types.isEmpty()==false) is false (no record was queried), which causes a null pointer exception in the next debug statement (system.debug(... type.RecordTypeId)).

5
  • Thanks for the response. Instantiated the standardController using the inserted accountSummary. Worked on everything else but overlooked copying structure from another method. So updated the unit test. Thanks. PS - debugs are being used to isolate the issue. Would not suggest eliminating use of Try / Catch.
    – rickmac
    Jul 26 '17 at 19:30
  • @rickmac You won't be able to test the catch block, because there's absolutely nothing to catch. If you don't know what you're trying to catch, don't do it.
    – sfdcfox
    Jul 26 '17 at 19:32
  • the catch will write to the VisualForce page: ApexPages.addMessages(ex);
    – rickmac
    Jul 26 '17 at 20:47
  • @rickmac My point is, there is no possible exception that will be thrown, so you can't get 100% coverage.
    – sfdcfox
    Jul 26 '17 at 21:23
  • There is an exception that was trapped. That was reported on the visualforce page.
    – rickmac
    Jul 28 '17 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.