0

I have a custom controller for a VF page that allows users to add and remove rows of records to create before they are actually created. My test class covers the addition of rows, but it does not cover the removal. I get the error:

System.NullPointerException: Argument cannot be null.

Class.NewRecController.RemoveType: line 32, column 1
Class.Test_NewRecController.testMethod1: line 29, column 1

The lines that are not being covered are 32-35 & 44-46 in my controller below. Can anyone help on how I can get these covered?

Test Class

@isTest 
private class Test_NewRecController {

    static testMethod void testMethod1()
    {
        Account testAccount = test_CreateRecords.createAcct(0);
        insert testAccount;
        
        Contact testContact = test_CreateRecords.createCont(testAccount.Id);
        insert testContact;
        
        PageReference pageRef = Page.NewRecType;
            pageRef.getParameters().put('contId','testContact.Id');
            pageRef.getParameters().put('acctId','testAccount.Id');
            pageRef.getParameters().put('Rec_Type__c','Type-1');

    Test.StartTest();
        
        NewRecController testNewRec = new NewRecController();
            testNewRec.saveType();
        
        NewRecController testNewRec2 = new NewRecController();
            testNewRec2.addRecType();

        NewRecController testNewRec3 = new NewRecController();
            testNewRec3.rec = 1;
            testNewRec3.removeType();

    Test.StopTest();
    }

}

Controller

public class NewRecController {

    Rec_Type__c rec1 = new Rec_Type__c();
    public list<Rec_Type__c> listRecType{ get; set; }

    Id acctId;
    Id contId;
    Public Integer rec {get; set;}
    Public List<Rec_Type__c> ContRecList {get; set;}
    
//    Constructor 

        public NewRecController() { 
            acctId = ApexPages.currentPage().getParameters().get('acctId');
            contId = ApexPages.currentPage().getParameters().get('contId');
            
            rec.Account__c = acctId;
            rec.Contact__c = contId;
    
            listRecType = new list<Rec_Type__c>();
            listRecType.add(rec1);
        } 
    
        Public void addRecType() {
            Rec_Type__c rec1 = new Rec_Type__c();
                rec1.Contact__c = contId;
                rec1.Account__c = acctId;
            listRecType.add(rec1);
            }
    
        public PageReference RemoveType() {
            rec = Integer.valueOf(ApexPages.currentPage().getParameters().get('rec'));
            listRecType.remove(rec);
            return null;
        }
         
        public PageReference saveType() {
            try {
                insert listRecType;
            } catch(DmlException e) {
                ApexPages.addMessages(e);
            return null;
            }
            PageReference contRecord = new PageReference('/'+contId);
            contRecord.setRedirect(true);
        return contRecord;
        }
    }

2 Answers 2

1

It looks like you need to call Test.setCurrentPage(pageRef); with an applicable page reference that has a rec query string parameter in your unit test. Right now there isn't a "current page" to access in your class. Without setting the page, it's not possible to get the parameters for it as you're attempting to do here:

rec = Integer.valueOf(ApexPages.currentPage().getParameters().get('rec'));

See this example of setting a page for the purposes of a test:

PageReference pageRef = Page.success;//Observe how we set the Page here
pageRef.getParameters().put('rec', 'some-value');
Test.setCurrentPage(pageRef);//Applying page context here

Additionally, it's a good practice to check what you're trying to pass to remove() or see if what you're attempting to remove exists within a container currently.

1
  • Ah, makes sense. I added that in and it worked. Thanks so much!
    – J. Neilan
    Sep 25, 2020 at 20:26
1

You can only pass index (Integer value) in list.remove method, but you are passing Rec_Type__c type in your apex class:

listRecType.remove(rec);

Please try following code for your test class:

@isTest 
    private class Test_NewRecController {
    
        static testMethod void testMethod1()
        {
            Account testAccount = test_CreateRecords.createAcct(0);
            insert testAccount;
            
            Contact testContact = test_CreateRecords.createCont(testAccount.Id);
            insert testContact;
            
            PageReference pageRef = Page.NewRecType;
                pageRef.getParameters().put('contId','testContact.Id');
                pageRef.getParameters().put('acctId','testAccount.Id');
                //pageRef.getParameters().put('Rec_Type__c','Type-1');
                pageRef.getParameters().put('rec1','0');
    
        Test.StartTest();
            
            NewRecController testNewRec = new NewRecController();
                testNewRec.saveType();
            
            NewRecController testNewRec2 = new NewRecController();
                testNewRec2.addRecType();
    
            NewRecController testNewRec3 = new NewRecController();
                //testNewRec3.rec = 1;
                testNewRec3.removeType();
    
        Test.StopTest();
        }
    
    }
3
  • Thanks. I tried that but still get the same error.
    – J. Neilan
    Sep 25, 2020 at 19:20
  • Have you updated listRecType.remove(rec); to listRecType.remove(rec1); in your apex class?
    – user1228
    Sep 25, 2020 at 19:24
  • Yes. That was an error and I changed it. Still no different.
    – J. Neilan
    Sep 25, 2020 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .