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
Shawn ReichnerShawn Reichner 

Help with Test Class not passing (List Has No Rows For Assignment)

I am in need of some great support form one of you Awesome Salesforce Guru's!

I have a Apex Controller that I am using as an extension on a VF page to handle incoming attachments and populate a look up field when the record is created from the VF page.  

I can not get the test class to not fail for the following reason System.QueryException: List has no rows for assignment to SObject and the stack trace is - Class.backlogAttachment.getReleaseMethod1: line 20, column 1
Class.backlogAttachment.<init>: line 12, column 1
Class.backlogAttachmentTest.backlogAttachmentTestMethod: line 10, column 1


Here is my controller class and Test class...can you please help me figure out why thsi is failing and what to do to fix it?  You woudl be a hge saviour here as I am new to apex but am trying my hardest!  Thanks in advance...

Shawn


Controller Extension:
 
public with sharing class backlogAttachment {
    
    public blob getfile{get;set;}
    public attachment objAttachment{get;set;}
    public Backlog__c objcase{get;set;}
    public string filename{get;set;}
    
    public backlogAttachment(apexpages.standardcontroller controller) {
    
        objcase = new Backlog__c();
        objAttachment = new Attachment();
        Scheduled_Releases__c sr = getReleaseMethod1();
        objcase.Scheduled_Release__c = sr.Id;
       }
    
    Scheduled_Releases__c s;
    
    public Scheduled_Releases__c getReleaseMethod1(){
    
        if(s == null) s=[SELECT Id, Name FROM Scheduled_Releases__c WHERE Name ='Software Enhancement Request (Default)' LIMIT 1];
        return s;
    }
    Public PageReference saveBacklog() {
    try{
        insert objcase;
    } catch(DMLException e) {
        ApexPages.addMessages(e);
        }
    if(filename != null && fileName.trim().length()>0 && getfile != null) {
        //objAttachment = new Attachment();
        Integer i=0;
        objAttachment.clear();
        objAttachment.body = getfile;
        objAttachment.ParentId = objcase.Id;
        objAttachment.name = this.filename;
        try{
        insert objAttachment;
          }catch(Exception e){
              system.debug(e);
              }
        }
        
        pagereference pr = new pagereference('/'+objcase.id);
        return pr;
    }


}

Test Class that is failing:
 
@isTest
public class backlogAttachmentTest {
    
    
    
    static testMethod void backlogAttachmentTestMethod() {
        List<Scheduled_Releases__c> re = [SELECT Id FROM Scheduled_Releases__c WHERE Id = 'a3l4B000000CiGw' LIMIT 1];
        Backlog__c temprec = new Backlog__c();
        ApexPages.StandardController cs = new ApexPages.StandardController(temprec);
        backlogAttachment controller = new backlogAttachment(cs);
        controller.getfile = Blob.valueof('Test Data');
        controller.filename = 'TestFieName';
        controller.objcase.Scheduled_Release__c = re[0].Id;
        test.startTest();
        	controller.saveBacklog();
        test.StopTest();
        
        
    }
    
    
    
}


    
Best Answer chosen by Shawn Reichner
Balayesu ChilakalapudiBalayesu Chilakalapudi
Create a record of type Scheduled_Releases__c 
Try like this,
@isTest
public class backlogAttachmentTest {  
    static testMethod void backlogAttachmentTestMethod() {
        Scheduled_Releases__c sr=new Scheduled_Releases__c();
        sr.Name='Software Enhancement Request (Default)';
        insert sr;
        Backlog__c temprec = new Backlog__c();
        ApexPages.StandardController cs = new ApexPages.StandardController(temprec);
        backlogAttachment controller = new backlogAttachment(cs);
        controller.getfile = Blob.valueof('Test Data');
        controller.filename = 'TestFieName';
        controller.objcase.Scheduled_Release__c = re[0].Id;
        test.startTest();
        	controller.saveBacklog();
        test.StopTest();  
    }      
}
Let us know if it helps you.
 

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Create a record of type Scheduled_Releases__c 
Try like this,
@isTest
public class backlogAttachmentTest {  
    static testMethod void backlogAttachmentTestMethod() {
        Scheduled_Releases__c sr=new Scheduled_Releases__c();
        sr.Name='Software Enhancement Request (Default)';
        insert sr;
        Backlog__c temprec = new Backlog__c();
        ApexPages.StandardController cs = new ApexPages.StandardController(temprec);
        backlogAttachment controller = new backlogAttachment(cs);
        controller.getfile = Blob.valueof('Test Data');
        controller.filename = 'TestFieName';
        controller.objcase.Scheduled_Release__c = re[0].Id;
        test.startTest();
        	controller.saveBacklog();
        test.StopTest();  
    }      
}
Let us know if it helps you.
 
This was selected as the best answer
Shawn ReichnerShawn Reichner
Bala, Thank you again kind sir.  After posting this post, I actually had the thought of adding (seeAllData = True) to the test class and that my friend did the trick!   I will keep your suggestion in mind as that looks like a great alternate.   Thanks again