+ Start a Discussion
JoshTonksJoshTonks 

Test Class Returning List has no rows for assignment

Hi I was hoping someone can help me.

Im trying to write an apex class test for my test class.
 
public class RenewalOpp_SavingsToDate {
        Id OpptyId = apexpages.currentpage().getparameters().get('id');
    	Id AcctId = [SELECT AccountId FROM Opportunity WHERE Id =: OpptyId LIMIT 1].Id;
    	Decimal SToD = [SELECT Savings_To_Data_ABS__c FROM Contact WHERE AccountId =: AcctId AND Do_The_Maths_Added__c = TRUE LIMIT 1].Savings_To_Data_ABS__c;
    	public Decimal Savings { get; set; }
        public RenewalOpp_SavingsToDate() {
            Savings = [SELECT Savings_To_Data_ABS__c FROM Contact WHERE AccountId =: AcctId AND Do_The_Maths_Added__c = TRUE LIMIT 1].Savings_To_Data_ABS__c;
        }
    
    List<Account> Acct;
    public List <Account> getAcct(){
      Acct = [SELECT id FROM Account WHERE id =: AcctId];
      return Acct;
    }   
    List<Contact> Cont;
    public List <Contact> getCont(){
      Cont = [SELECT id FROM Contact WHERE AccountId =: AcctId AND Do_The_Maths_Added__c = TRUE LIMIT 1];
      return Cont;
    }    
    

}
When i am trying to test it with the test class below all i get is List has no rows for assignment
@istest(SeeAllData=false)
public class RenewalOpp_SavingsToDate_Test {

  static testMethod void testSetUp() {
     
  Account acc = new Account();
  acc.Name = 'Test';
  acc.Type = 'Customer';
  insert acc;
      
  Contact con = new Contact();
  con.LastName='Test01';
  con.Email='Test01@Test01.com';
  con.Phone='0234 123456';
  con.AccountId=acc.Id;
  con.Do_The_Maths_Added__c = TRUE; 
  insert con;
      
  Opportunity opp = new Opportunity();
  opp.Name = 'Test';
  opp.StageName = 'Not closed';
  opp.CloseDate = Date.Today();
  opp.AccountId = acc.id;
  opp.Type = 'New Business';
  opp.Opp_Enquiry_Source__c = 'Test';
  opp.User__c = UserInfo.GetUserId();
  opp.OwnerId = opp.User__c;
  insert opp;
      
  Order__c o = new Order__C();
  o.Proposal_Reference__c = opp.Id;
  o.Number_of_Units__c = 1.0;
  o.Contract_Start_date__c = Date.Today();
  o.Contract_End_date__c = Date.Today();
  o.Company_Name__c = acc.Id;
  insert o;
      
  Id OpptyId = opp.Id;
      
  //instance of class
  RenewalOpp_SavingsToDate ins = new RenewalOpp_SavingsToDate();
  List <Account>caseList = ins.getAcct();
  List <Contact>caseList2 = ins.getCont();

  // assert
  System.assertEquals(1, caseList.size());
  System.assertEquals(1, caseList2.size());
  }
    
}
I dont understand why it is returning the no rows when ive just created them in the test class.

 
ShirishaShirisha (Salesforce Developers) 

Hi,

Greetings!

I would suggest you to add the system.debug() statements to check if the records are getting inserted or not.

Also,please capture the debug logs while running the class to see the complete execution.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri