1

I am setting up the test class for a custom controller, but running into a null pointer exception after the controller has been set running the deleteRow method. I'm not sure what else I need to pass in. Also, even though I am obtaining the required code coverage, I feel like I should be doing some system.asserts in order to do actual tests, rather than just running the methods. Can you please point me in the right direction for what additional arguments are needed in order to have sound test coverage for this class?

Error:

The error is when I call ctrl.deleterow() in my test:

System.NullPointerException: Argument cannot be null

Controller:

public class fieldDynamicExtension {

    public List<FieldExam__c> delexamList = new List<FieldExam__c>();
    public List<FieldExam__c> examList                  {get;set;}
    public Loan_Memo__c myMemo                          {get;set;}
    public  LLC_BI__Loan__c myLoan                      {get;set;}
    public FieldExam__c myExam                          {get;set;}
    public Integer rowIndex                             {get;set;}  

public fieldDynamicExtension() {     
    //Set the fields via fieldset
    this.mymemo = getInputs(); 
    //If the page is accessed from record, pull in the loan and relationship to related memo fields
    String currentRecordID = ApexPages.CurrentPage().getparameters().get('id');
    if (currentRecordID != null){
        myLoan  = [select id, LLC_BI__Account__c from LLC_BI__Loan__c where id =: currentRecordId];
        myMemo.Loan__c = myLoan.id;
        myMemo.Relationship__c = myLoan.LLC_BI__Account__c;}

    // Set the other memo field values and bind the field exam results to the id of the memo.

    myMemo.Name = 'Field Exam';
    myMemo.RecordTypeId = [Select Id From RecordType Where SObjectType = 'Loan_Memo__c' And DeveloperName='Field_Exam_Memo'].Id;
    myMemo.As_of__c = date.today();

    examList = [Select id, Field_Examiner_Recommendation__c, Relationship_Manager_Response__c from FieldExam__c where LoanMemo__c =: myMemo.Id];
    Integer totalCount = examList.size();   
    delExamList  = new List<FieldExam__c>();}    
 public List<Schema.FieldSetMember> getFields() {
    return SObjectType.Loan_Memo__c.FieldSets.FieldExamHeader.getFields();}
 public Loan_Memo__c getInputs() {
    String query = 'SELECT ';
    for(Schema.FieldSetMember memoinfo : this.getFields()) {
        query += memoinfo.getFieldPath() + ', ';
    }
    query += 'Id FROM Loan_Memo__c LIMIT 1';
    return Database.query(query);
}
public void addRow(){
            examList.add(new FieldExam__c(LoanMemo__c = myMemo.Id));}
public ApexPages.PageReference keep(){
 upsert mymemo;
    for (FieldExam__c Exams : examList){
        Exams.LoanMemo__c = myMemo.Id;}
  upsert examList;
  return (new ApexPages.StandardController(myMemo)).Save();   }      
public PageReference returnToLoan(){
    PageReference page = new PageReference('/'+myLoan.Id);    
    page.setRedirect(true);
    return page; }       
public void deleteRow(){
    rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
    if (myExam.id != null) {
            myExam = examList.remove(rowIndex);
            delexamList.add(myExam);}
    }
}

Test:

@isTest
public class fieldDynamicExtensionTest {

    @isTest    
    static void testFieldDynamicExtension(){

        //Create Loan, Relationship, Loan Memo, Field Exam
        Account myAcct = New Account (Name = 'TestAcct');
        insert myAcct;

        LLC_BI__Loan__c myLoan = New LLC_BI__Loan__c(Name = 'TestLoan', LLC_BI__Account__c=myAcct.id);
        insert myLoan;

        Loan_Memo__c myMemo = New Loan_Memo__c(Name = 'TestMemo', Loan__c = myLoan.id, Relationship__c = myLoan.LLC_BI__Account__c);
        insert myMemo;

        FieldExam__c myExam = New FieldExam__c(LoanMemo__c = myMemo.Id);
        insert myExam;

        //Instantiate VF Page
        PageReference pg = Page.FieldExam2;
        Test.setCurrentPage(pg);
        String currentRecordID = ApexPages.currentPage().getParameters().put('id', myLoan.id); 

         // Instantiate the controller run the methods

        fieldDynamicExtension ctrl = new fieldDynamicExtension();
        ctrl.addRow();
        ctrl.keep();
        ctrl.returnToLoan();
        ctrl.deleteRow();  
    }
}
2

You need to put an Integer in the rowIndex parameter before calling delteRow.

ApexPages.currentPage().getParameters().put('rowIndex', '1');

I would recommend refactoring that method to check if the parameter is actually set. Something like:

Integer rowIndex;
String indexParam = ApexPage.currentPage().getParameters().get('rowIndex');
if (String.isBlank(indexParam)) return;

try { rowIndex = Integer.valueOf(indexParam); }
catch (TypeException t) { return; }

// logic depending on rowIndex

Other than that, the most pressing issue with your code is that your test is only a smoke test (containing no assertions). Take a look at How to Write Good Unit Tests (emphasis mine):

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

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.