function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lauren BLauren B 

List has no rows for assignment to SObject - Test Class

Hi,
I have written following apex class and test class. I'm getting an error saying "List has no rows for assignment to SObject" and my test class is getting failed though my code coverage is 91%. I'd appreciate any assistance.



public  class MMAResultReportController

{
    public List<DOAS_AVS_RESPONSE_LEVEL_1__c> avsresults {get;set;}
    public Test_MMA2__c avsRequest {get;set;}
    public string strRequestID{get;set;}
    
    //public string searchRef{get;set;}
    public  MMAResultReportController(){
        system.debug('####Printing page reference');
        system.debug(System.currentPageReference().getParameters().get('RequestID'));
        strRequestID=System.currentPageReference().getParameters().get('RequestID');
        
        getavsRequest();
    }
    public void getavsRequest(){
        avsRequest= new Test_MMA2__c();
        //system.debug ('####strRequestID is: '+strRequestID);
        //system.debug('####Variable is: ' + strRequestID);
        avsRequest = [select MMA_Request_ID__c,MMA_RecordType__c,MMA_Suffix__c,MMA_DOB__c,MMA_Middle_Name__c,MMA_Flag__c,MMA_Sex__c,MMA_SSN__c,MMA_First_Name__c, MMA_Last_Name__c, MMA_Family_ID__c, MMAReportDate__c,MMA_BENE_SSN1__c,
from Test_MMA2__c where MMA_Request_ID__c =:strRequestID];//                                                                 
    }
    
           
    
}


Test Class-

@isTest(SeeAllData=true)
public class MMAResultReportController_Test {
    @istest static void test(){

        Test_MMA2__c tes= new Test_MMA2__c ();
        tes.name='Sample';
        tes.MMA_First_Name__c='John';
        tes.MMA_Last_Name__c='Smith';
        tes.MMAReportDate__c=date.today();
        tes.MMA_Middle_Name__c='De';
       
        insert tes;
        test.startTest();
     
       MMAResultReportController ce= new MMAResultReportController();
      
        ce.getavsRequest();
        test.stopTest();
    }

 
Best Answer chosen by Lauren B
Abdul KhatriAbdul Khatri
Hi Lauren B

There are two ways to solve your issue.

Option 1:
If you just want to have your issue address then use the following code. In the below code I just made the SOQL result to List<Test_MMA2__c>. Since from your test class that SOQL is not returning any record so this will through that normal error, changing the result to List will solve the issue but you will receive the null return which you need to handle accordingly.
 
public  class MMAResultReportController{
    public List<DOAS_AVS_RESPONSE_LEVEL_1__c> avsresults {get;set;}
    public List<Test_MMA2__c> avsRequest {get;set;}
    public string strRequestID{get;set;}
    
    //public string searchRef{get;set;}
    public  MMAResultReportController(){
        system.debug('####Printing page reference');
        system.debug(System.currentPageReference().getParameters().get('RequestID'));
        strRequestID=System.currentPageReference().getParameters().get('RequestID');
        
        getavsRequest();
    }
    
    public void getavsRequest(){
        avsRequest= new List<Test_MMA2__c>();
        //system.debug ('####strRequestID is: '+strRequestID);
        //system.debug('####Variable is: ' + strRequestID);
        avsRequest = [select MMA_Request_ID__c,MMA_RecordType__c,MMA_Suffix__c,MMA_DOB__c,MMA_Middle_Name__c,MMA_Flag__c,MMA_Sex__c,MMA_SSN__c,MMA_First_Name__c, MMA_Last_Name__c, MMA_Family_ID__c, MMAReportDate__c,MMA_BENE_SSN1__c,
						from Test_MMA2__c where MMA_Request_ID__c =:strRequestID];//                                                                 
    }
}


Option 2
In the Test Class insert a record for Test_MMA2__c with the value MMA_Request_ID__c. Since I am expecting you are using this controller in your Visualforce Page let say Page1 (User your one). Use the following code in your test class. Using this way you don't need to change your main class

@isTest
public class MMAResultReportController_Test {
    
    @istest static void test(){

        Test_MMA2__c tes= new Test_MMA2__c ();
        tes.name='Sample';
        tes.MMA_First_Name__c='John';
        tes.MMA_Last_Name__c='Smith';
        tes.MMAReportDate__c=date.today();
        tes.MMA_Middle_Name__c='De';
        tes.MMA_Request_ID__c = 'Test';
        insert tes;
        
        test.startTest();
        PageReference pref = Page.Page1; //Use your visualforce page name
        pref.getParameters().put('RequestID', tes.MMA_Request_ID__c);
        Test.setCurrentPage(pref);
       	MMAResultReportController ce= new MMAResultReportController();      
        ce.getavsRequest();
        test.stopTest();
    }

    //Some system.assert ....
}
Also as a best practise it is always good to use system.assert making test more robust with the expected values.
Also I see  getavsRequest should be returning either List<Test_MMA2__c > (From Option1) or Test_MMA2__c (From Option 2) based on what option you are selecting

I hope this help for you to take away something moving forward.
 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,

You are getting this error because your query is not able to select the fields that you have fetched in the main class where :-
MMA_Request_ID__c =:strRequestID

In your test class, you must define the look-up of tes.MMA_Request_ID__c =  required strrequestid;

Please mark it as best answer if it helps you,

Thanks & Regards,
Suraj Tripathi

 
Abdul KhatriAbdul Khatri
Hi Lauren B

There are two ways to solve your issue.

Option 1:
If you just want to have your issue address then use the following code. In the below code I just made the SOQL result to List<Test_MMA2__c>. Since from your test class that SOQL is not returning any record so this will through that normal error, changing the result to List will solve the issue but you will receive the null return which you need to handle accordingly.
 
public  class MMAResultReportController{
    public List<DOAS_AVS_RESPONSE_LEVEL_1__c> avsresults {get;set;}
    public List<Test_MMA2__c> avsRequest {get;set;}
    public string strRequestID{get;set;}
    
    //public string searchRef{get;set;}
    public  MMAResultReportController(){
        system.debug('####Printing page reference');
        system.debug(System.currentPageReference().getParameters().get('RequestID'));
        strRequestID=System.currentPageReference().getParameters().get('RequestID');
        
        getavsRequest();
    }
    
    public void getavsRequest(){
        avsRequest= new List<Test_MMA2__c>();
        //system.debug ('####strRequestID is: '+strRequestID);
        //system.debug('####Variable is: ' + strRequestID);
        avsRequest = [select MMA_Request_ID__c,MMA_RecordType__c,MMA_Suffix__c,MMA_DOB__c,MMA_Middle_Name__c,MMA_Flag__c,MMA_Sex__c,MMA_SSN__c,MMA_First_Name__c, MMA_Last_Name__c, MMA_Family_ID__c, MMAReportDate__c,MMA_BENE_SSN1__c,
						from Test_MMA2__c where MMA_Request_ID__c =:strRequestID];//                                                                 
    }
}


Option 2
In the Test Class insert a record for Test_MMA2__c with the value MMA_Request_ID__c. Since I am expecting you are using this controller in your Visualforce Page let say Page1 (User your one). Use the following code in your test class. Using this way you don't need to change your main class

@isTest
public class MMAResultReportController_Test {
    
    @istest static void test(){

        Test_MMA2__c tes= new Test_MMA2__c ();
        tes.name='Sample';
        tes.MMA_First_Name__c='John';
        tes.MMA_Last_Name__c='Smith';
        tes.MMAReportDate__c=date.today();
        tes.MMA_Middle_Name__c='De';
        tes.MMA_Request_ID__c = 'Test';
        insert tes;
        
        test.startTest();
        PageReference pref = Page.Page1; //Use your visualforce page name
        pref.getParameters().put('RequestID', tes.MMA_Request_ID__c);
        Test.setCurrentPage(pref);
       	MMAResultReportController ce= new MMAResultReportController();      
        ce.getavsRequest();
        test.stopTest();
    }

    //Some system.assert ....
}
Also as a best practise it is always good to use system.assert making test more robust with the expected values.
Also I see  getavsRequest should be returning either List<Test_MMA2__c > (From Option1) or Test_MMA2__c (From Option 2) based on what option you are selecting

I hope this help for you to take away something moving forward.
 

This was selected as the best answer
Lauren BLauren B
Hi Abdul Khatri,
Thank you so much for your reply and letting me know the best practices. I really appreciate your help.
Lauren BLauren B
Hi Abdul Khatri,
I didn't check the out come, just checked my test class is not showing any error after implementing your code. But in first case its not showing any record and in second case (in test class) showing tes.MMA_Request_ID__c is writable. Please assist.
Abdul KhatriAbdul Khatri
Hi Lauren,

In first case it is not showing any record because the SOQL return no result based on the filter as there is no record for that MMA_Request_ID__c.

In the second, since I do not have your Data Model, I am not sure how MMA_Request_ID__c is defined. I just assume it is input text field but if it is anything different please try to get the value in that field and then your below line should work 
pref.getParameters().put('RequestID', tes.MMA_Request_ID__c);
otherwise you can use it like putting the exact value like this
pref.getParameters().put('RequestID', 'Test');

I hope this clarifies. In case not please send me the screen shot of the exact issue.