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
StaciStaci 

@isTest class help

I have the following test class and its erroring with a list has no rows for assignment to SObject and it gives this line number
CW_alertUpdate controller = new CW_alertUpdate(sc); //This is to initialize your class

Not sure what I'm missing, I'm a newbie to test classes
 
@isTest
private class CW_alertUpdateTest {

static testMethod void CW_alertUpdateTest() {
//creates new alert
Alerts__c a1 = new Alerts__c();
a1.Account_Name__c = '00119000003QPkD';
a1.Subject__c = 'This is an Alert';
a1.Alert_Status__c = 'Initial';
a1.Product__c = 'Fleet';
a1.Customer_Impact__c = 'This is a huge impact to site';
a1.Completed_Actions__c = 'This is what I have done';
a1.Next_Actions__c = 'This is what I will do next';
a1.Incident_Ticket_Number__c='50019000000zkD4';
//a1.Incident_Reported__c==Date.today();
a1.Incident_Start__c=Date.today();
a1.Incident_Manager__c='005a00000086FDq';
a1.Team_List__c='Matt Mansfield';
a1.CGM_P1_APD_MANNED__c=true;
a1.CGM_P1_Command__c=false;
a1.CGM_P1_EAME_MANNED__c=false;
a1.CGM_P1_AMERICAS_MANNED__c=false;
a1.CGM_P1_SOUTH_AMERICAS_MANNED__c=false;
//a1.Master_AlertText__c='';
insert a1;

//creates new alert
Alerts__c a2 = new Alerts__c();
a2.Account_Name__c = a1.Account_Name__c;
a2.Subject__c = a1.Subject__c;
a2.Alert_Status__c = 'Update';
a2.Product__c = a1.Product__c;
a2.Customer_Impact__c = a1.Customer_Impact__c;
a2.Completed_Actions__c = a1.Completed_Actions__c;
a2.Next_Actions__c = a1.Next_Actions__c;
a2.Incident_Ticket_Number__c=a1.Incident_Ticket_Number__c;
//a2.Incident_Reported__c==Date.today();
a2.Incident_Start__c=a1.Incident_Start__c;
a2.Incident_Manager__c=a1.Incident_Manager__c;
a2.Team_List__c=a1.Team_List__c;
a2.CGM_P1_APD_MANNED__c=a1.CGM_P1_APD_MANNED__c;
a2.CGM_P1_Command__c=a1.CGM_P1_Command__c;
a2.CGM_P1_EAME_MANNED__c=a1.CGM_P1_EAME_MANNED__c;
a2.CGM_P1_AMERICAS_MANNED__c=a1.CGM_P1_AMERICAS_MANNED__c;
a2.CGM_P1_SOUTH_AMERICAS_MANNED__c=a1.CGM_P1_SOUTH_AMERICAS_MANNED__c;
a2.Master_AlertText__c=a1.Name;
insert a2;



//test.startTest();
           ApexPages.StandardController sc = new ApexPages.StandardController(a1);
           CW_alertUpdate controller = new CW_alertUpdate(sc); //This is to initialize your class
      
pageReference retPage = Page.CW_AlertUpdate; 

ID alertID = retPage.getParameters().put('newid',a2.ID); 
ID alertID1 = retPage.getParameters().put('alertID',a1.ID); 



Test.setCurrentPage(retPage);
retPage = controller.autoRun();

//test.stopTest();

}}
 
public class CW_alertUpdate{

public final Alerts__c alertID;
public Alerts__c newAlert{get;set;}
   
    public CW_alertUpdate(ApexPages.StandardController controller) {
        //gets fields from previous case
        alertID = [SELECT Id, Master_AlertText__c, Incident_Ticket_Number__c FROM Alerts__c WHERE Id = :ApexPages.currentPage().getParameters().get('alertID')];
      }
     public pageReference autorun(){
         //create a new Alert
         Alerts__c newAlert = new Alerts__c(Id = ApexPages.currentPage().getParameters().get('newid'));
          
          // Fill in the values for the new record from previous case
        if(alertID.Master_AlertText__c == NULL)
        {
         newAlert.Master_AlertText__c = alertID.Id;
         }
         //Update the new Alert record
        update newAlert;
        system.debug(newAlert);
     
        
        //create a new Case Alert Association - Junction Object
        Case_Alert_Association__c newCAA = new Case_Alert_Association__c();
         // Fill in the values for the new record
         newCAA.Alerts__c = newAlert.id;
         newCAA.Case__c = alertID.Incident_Ticket_Number__c;
                
        //Insert the new Case Alert Association record
        Database.insert (newCAA);
        system.debug(newCAA);
                    
        
        system.debug(newAlert);
        PageReference retPage = new PageReference('/' + newAlert.id);
        return retPage; 
        
        
       }


    }




 
Best Answer chosen by Staci
Roy LuoRoy Luo
In the unit test, controller is initialized before setting up the VF page, thus pageParameter alertID would be null thus the soql query will return nothing.

Need to move the controller down a few lines after initializing your VF page.
 
@isTest
private class CW_alertUpdateTest {

static testMethod void CW_alertUpdateTest() {
//creates new alert
Alerts__c a1 = new Alerts__c();
a1.Account_Name__c = '00119000003QPkD';
a1.Subject__c = 'This is an Alert';
a1.Alert_Status__c = 'Initial';
a1.Product__c = 'Fleet';
a1.Customer_Impact__c = 'This is a huge impact to site';
a1.Completed_Actions__c = 'This is what I have done';
a1.Next_Actions__c = 'This is what I will do next';
a1.Incident_Ticket_Number__c='50019000000zkD4';
//a1.Incident_Reported__c==Date.today();
a1.Incident_Start__c=Date.today();
a1.Incident_Manager__c='005a00000086FDq';
a1.Team_List__c='Matt Mansfield';
a1.CGM_P1_APD_MANNED__c=true;
a1.CGM_P1_Command__c=false;
a1.CGM_P1_EAME_MANNED__c=false;
a1.CGM_P1_AMERICAS_MANNED__c=false;
a1.CGM_P1_SOUTH_AMERICAS_MANNED__c=false;
//a1.Master_AlertText__c='';
insert a1;

//creates new alert
Alerts__c a2 = new Alerts__c();
a2.Account_Name__c = a1.Account_Name__c;
a2.Subject__c = a1.Subject__c;
a2.Alert_Status__c = 'Update';
a2.Product__c = a1.Product__c;
a2.Customer_Impact__c = a1.Customer_Impact__c;
a2.Completed_Actions__c = a1.Completed_Actions__c;
a2.Next_Actions__c = a1.Next_Actions__c;
a2.Incident_Ticket_Number__c=a1.Incident_Ticket_Number__c;
//a2.Incident_Reported__c==Date.today();
a2.Incident_Start__c=a1.Incident_Start__c;
a2.Incident_Manager__c=a1.Incident_Manager__c;
a2.Team_List__c=a1.Team_List__c;
a2.CGM_P1_APD_MANNED__c=a1.CGM_P1_APD_MANNED__c;
a2.CGM_P1_Command__c=a1.CGM_P1_Command__c;
a2.CGM_P1_EAME_MANNED__c=a1.CGM_P1_EAME_MANNED__c;
a2.CGM_P1_AMERICAS_MANNED__c=a1.CGM_P1_AMERICAS_MANNED__c;
a2.CGM_P1_SOUTH_AMERICAS_MANNED__c=a1.CGM_P1_SOUTH_AMERICAS_MANNED__c;
a2.Master_AlertText__c=a1.Name;
insert a2;



//test.startTest();

pageReference retPage = Page.CW_AlertUpdate; 

ID alertID = retPage.getParameters().put('newid',a2.ID); 
ID alertID1 = retPage.getParameters().put('alertID',a1.ID); 

Test.setCurrentPage(retPage);

ApexPages.StandardController sc = new ApexPages.StandardController(a1);
CW_alertUpdate controller = new CW_alertUpdate(sc);
 //This is to initialize your class
      


retPage = controller.autoRun();

//test.stopTest();

}}

 

All Answers

James LoghryJames Loghry
Your constructor depends on an http parameter called 'alertID'.  You need to specify this attribute in your test class before calling your constructor.  For example:
 
ApexPages.currentPage().getParameters().put('alertID',a1.Id);
ApexPages.StandardController sc = new ApexPages.StandardController(a1);

Alternatively, you could (and should) update your constructor to use the standard way of fetching the record from the standard controller:
 
public CW_alertUpdate(ApexPages.StandardController controller) {
        //gets fields from previous case
        alertID = [SELECT Id, Master_AlertText__c, Incident_Ticket_Number__c FROM Alerts__c WHERE Id = :controller.getId()];
      }
Keep in mind, this uses the id= parameter instead of alertID parameter.
Roy LuoRoy Luo
In the unit test, controller is initialized before setting up the VF page, thus pageParameter alertID would be null thus the soql query will return nothing.

Need to move the controller down a few lines after initializing your VF page.
 
@isTest
private class CW_alertUpdateTest {

static testMethod void CW_alertUpdateTest() {
//creates new alert
Alerts__c a1 = new Alerts__c();
a1.Account_Name__c = '00119000003QPkD';
a1.Subject__c = 'This is an Alert';
a1.Alert_Status__c = 'Initial';
a1.Product__c = 'Fleet';
a1.Customer_Impact__c = 'This is a huge impact to site';
a1.Completed_Actions__c = 'This is what I have done';
a1.Next_Actions__c = 'This is what I will do next';
a1.Incident_Ticket_Number__c='50019000000zkD4';
//a1.Incident_Reported__c==Date.today();
a1.Incident_Start__c=Date.today();
a1.Incident_Manager__c='005a00000086FDq';
a1.Team_List__c='Matt Mansfield';
a1.CGM_P1_APD_MANNED__c=true;
a1.CGM_P1_Command__c=false;
a1.CGM_P1_EAME_MANNED__c=false;
a1.CGM_P1_AMERICAS_MANNED__c=false;
a1.CGM_P1_SOUTH_AMERICAS_MANNED__c=false;
//a1.Master_AlertText__c='';
insert a1;

//creates new alert
Alerts__c a2 = new Alerts__c();
a2.Account_Name__c = a1.Account_Name__c;
a2.Subject__c = a1.Subject__c;
a2.Alert_Status__c = 'Update';
a2.Product__c = a1.Product__c;
a2.Customer_Impact__c = a1.Customer_Impact__c;
a2.Completed_Actions__c = a1.Completed_Actions__c;
a2.Next_Actions__c = a1.Next_Actions__c;
a2.Incident_Ticket_Number__c=a1.Incident_Ticket_Number__c;
//a2.Incident_Reported__c==Date.today();
a2.Incident_Start__c=a1.Incident_Start__c;
a2.Incident_Manager__c=a1.Incident_Manager__c;
a2.Team_List__c=a1.Team_List__c;
a2.CGM_P1_APD_MANNED__c=a1.CGM_P1_APD_MANNED__c;
a2.CGM_P1_Command__c=a1.CGM_P1_Command__c;
a2.CGM_P1_EAME_MANNED__c=a1.CGM_P1_EAME_MANNED__c;
a2.CGM_P1_AMERICAS_MANNED__c=a1.CGM_P1_AMERICAS_MANNED__c;
a2.CGM_P1_SOUTH_AMERICAS_MANNED__c=a1.CGM_P1_SOUTH_AMERICAS_MANNED__c;
a2.Master_AlertText__c=a1.Name;
insert a2;



//test.startTest();

pageReference retPage = Page.CW_AlertUpdate; 

ID alertID = retPage.getParameters().put('newid',a2.ID); 
ID alertID1 = retPage.getParameters().put('alertID',a1.ID); 

Test.setCurrentPage(retPage);

ApexPages.StandardController sc = new ApexPages.StandardController(a1);
CW_alertUpdate controller = new CW_alertUpdate(sc);
 //This is to initialize your class
      


retPage = controller.autoRun();

//test.stopTest();

}}

 
This was selected as the best answer