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
Matthew A SmallMatthew A Small 

My Test class has me stumped!

Hi everyone. 
I am relatively new to writing test classes and I am creating a package. 

I get this error when I try to upload into the package. 
User-added image

Here is my test class

@isTest
private class CheckInShiftCreateCtrlTest {
  
  
  @isTest static void testConstructor_ParentShiftExist() {
    Shift__c shift= new Shift__c();
    insert shift;
    shift= [select Id, Name from Shift__c where id = : shift.id];
    

    Contact_Report__c contRepForInsert = new Contact_Report__c(
      Shift__c = shift.Id
    );
    insert contRepForInsert;

    Test.setCurrentPage(Page.CheckInCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
    
    Test.startTest();
        ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
    CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
    Test.stopTest();

    System.assertEquals(shift.Id, ctrl.contactRep.Shift__c);
  }
  
  @isTest static void testConstructor_NoParentShift() {
    Contact_Report__c contRepForInsert = new Contact_Report__c();
    insert contRepForInsert;

    Test.setCurrentPage(Page.CheckInCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
    
    Test.startTest();
        ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
    CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
    Test.stopTest();

    System.assert(ctrl.contactRep != null);
  }

  @isTest static void testRefreshFilterResult() {
    
    Shift__c shift2= new Shift__c();
    insert shift2;
    shift2= [select Id, Name from Shift__c where id = : shift2.id];
    
    Shift__c contRepForInsert = new shift__c();
    insert contRepForInsert;
    contRepForInsert= [select Id, Name from Shift__c where id = : contRepForInsert.id];
    
      

    Test.setCurrentPage(Page.CheckInCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
    
    Test.startTest();
        ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
    CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
    ctrl.selectedShiftName = 'test';
    ctrl.refreshFilterResults();
    Integer countRecords1 = ctrl.allFilteredShift.size();

    ctrl.selectedShiftName = '';
    ctrl.refreshFilterResults();
    Integer countRecords2 = ctrl.allFilteredShift.size();

    ctrl.selectedShiftName = 'testShift2';
    ctrl.refreshFilterResults();
    Integer countRecords3 = ctrl.allFilteredShift.size();  

    Test.stopTest();

    System.assertEquals(3, countRecords1);
    System.assertEquals(3, countRecords2);
    System.assertEquals(2, countRecords3);
  }

  @isTest static void testSelectShift() {
    Shift__c shift = new Shift__c();
    insert shift ;
    shift= [select Id, Name from Shift__c where id = : shift.id];
    Shift__c shift2 = new Shift__c();
    shift2= [select Id, Name from Shift__c where id = : shift2.id];
    insert new List<Shift__c>{shift, shift2};


Shift__c contRepForInsert = new Shift__c();
       insert contRepForInsert ;
    contRepForInsert = [select Id, Name from Shift__c where id = : contRepForInsert .id];
    

    Test.setCurrentPage(Page.ContactReportCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
    
    Test.startTest();
        ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
    CheckInCreateCtrl ctrl = new CheckInCreateCtrl(sc);
    
     
    ctrl.refreshFilterResults();
    ApexPages.CurrentPage().getparameters().put('shiftName', 'testShift2');
    
    Test.stopTest();

    System.assertEquals(shift.Id, ctrl.contactRep.Shift__c);
  }
  }




Can anyone help? 
 
Matthew A SmallMatthew A Small
Sorry errors are 

CheckInShiftCreateCtrlTest.testRefreshFilterResultSystem.TypeException: Invalid conversion from runtime type Shift__c to Contact_Report__cClass.CheckInShiftCreateCtrl.<init>: line 12, column 1 Class.CheckInShiftCreateCtrlTest.testRefreshFilterResult: line 59, column 1

CheckInShiftCreateCtrlTest.testSelectShiftSystem.QueryException: List has no rows for assignment to SObjectClass.CheckInShiftCreateCtrlTest.testSelectShift: line 84, column 1
jigarshahjigarshah
Matt,

One thing I notice is that everywhere in your test code you are attempting to assign the result of your Soql Query to a single Sobject instance instead of a List of Sobjects. The error List has no rows for assignment to Sobject occurs when you attempt to assign the result of your Soql Query to a single Sobject instance and the query result does not return an records matching the given criteria.

Update the code within the testSelectShift() method as follows as well as the other test methods.
@isTest static void testSelectShift() {
    
	//Create multiple Shift records
	List<Shift__c> shiftList = new List<Shift__c>();
	
	for(Int shiftCnt = 0; shiftCnt < 10; shiftCnt++){
		shiftList.add(new Shift__c());
	}
	
	List<Database.SaveResult> shiftSaveResultList = Database.insert(shiftList);
	
	//Get the Ids of the inserted shift records
	Set<Id> shiftIdSet = new Set<Id>();
	
	for(Database.SaveResult saveResult :shiftSaveResultList){
		if(saveResult.isSuccess()){ shiftIdSet.add(saveResult.getId()); }
	}
	
	//Retrieve the newly created Shift records
	List<Shift__c> newShiftRecList = [select Id, Name From Shift__c Where Id IN :shiftIdSet];

	Shift__c contRepForInsert = new Shift__c();
    List<Database.SaveResult> contRepSaveResultList = Database.insert(contRepForInsert);
    List<Shift__c> contRepForInsertList = [Select Id, Name From Shift__c where Id =   :contRepSaveResultList[0].getId()];

    Test.setCurrentPage(Page.ContactReportCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsertList[0].Id);
    
    Test.startTest();
    
		ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsertList[0]);
		CheckInCreateCtrl ctrl = new CheckInCreateCtrl(sc);
		ctrl.refreshFilterResults();
		ApexPages.CurrentPage().getparameters().put('shiftName', 'testShift2');
    
    Test.stopTest();

    System.assertEquals(newShiftRecList[0].Id, ctrl.contactRep.Shift__c);
}
Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
Matthew A SmallMatthew A Small
Hey thanks for the help but i now have this error


Error: Compile Error: Invalid type: Int at line 84 column 9

for(Int shiftCnt = 0; shiftCnt < 10; shiftCnt++){     is the line in question. 
Do you know what went wrong here? 

Thanks again, 
Matt 
jigarshahjigarshah
Replace int with integer. Regards, Jigar Shah
Matthew A SmallMatthew A Small
Hi Jigar, 

The "integer fix looked like it did the trick but I have another compile error now.


Error: Compile Error: Illegal assignment from Database.SaveResult to List<Database.SaveResult> at line 101 column 33

      List<Database.SaveResult> contRepSaveResultList = Database.insert(contRepForInsert);

=(
jigarshahjigarshah
Modify the above line of code as wel as the subsequent line to as below.
Database.SaveResult contRepSaveResultList = Database.insert(contRepForInsert);
    List<Shift__c> contRepForInsertList = [Select Id, Name From Shift__c where Id = :contRepSaveResultList.getId()];
Matthew A SmallMatthew A Small
Ok so that worked and I could save the test class successfully however when I package up onceagain I have this error...

CheckInShiftCreateCtrlTest.testRefreshFilterResultSystem.TypeException: Invalid conversion from runtime type Shift__c to Contact_Report__cClass.CheckInShiftCreateCtrl.<init>: line 12, column 1 Class.CheckInShiftCreateCtrlTest.testRefreshFilterResult: line 59, column 1
CheckInShiftCreateCtrlTest.testSelectShiftSystem.TypeException: Invalid conversion from runtime type Shift__c to Contact_Report__cClass.CheckInCreateCtrl.<init>: line 12, column 1 Class.CheckInShiftCreateCtrlTest.testSelectShift: line 110, column 1
jigarshahjigarshah
Matthew,

Please share the complete code snippet that you have. Also, please use the "< > icon" in the answer editor to paste the code snippet.
Matthew A SmallMatthew A Small
@isTest
private class CheckInShiftCreateCtrlTest {
  
  
  @isTest static void testConstructor_ParentShiftExist() {
    Shift__c shift= new Shift__c();
    insert shift;
    shift= [select Id, Name from Shift__c where id = : shift.id];
    

    Contact_Report__c contRepForInsert = new Contact_Report__c(
      Shift__c = shift.Id
    );
    insert contRepForInsert;

    Test.setCurrentPage(Page.CheckInCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
    
    Test.startTest();
        ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
    CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
    Test.stopTest();

    System.assertEquals(shift.Id, ctrl.contactRep.Shift__c);
  }
  
  @isTest static void testConstructor_NoParentShift() {
    Contact_Report__c contRepForInsert = new Contact_Report__c();
    insert contRepForInsert;

    Test.setCurrentPage(Page.CheckInCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
    
    Test.startTest();
        ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
    CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
    Test.stopTest();

    System.assert(ctrl.contactRep != null);
  }

  @isTest static void testRefreshFilterResult() {
    
    Shift__c shift2= new Shift__c();
    insert shift2;
    shift2= [select Id, Name from Shift__c where id = : shift2.id];
    
    Shift__c contRepForInsert = new shift__c();
    insert contRepForInsert;
    contRepForInsert= [select Id, Name from Shift__c where id = : contRepForInsert.id];
    
      

    Test.setCurrentPage(Page.CheckInCreate);
    ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
    
    Test.startTest();
        ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
    CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
    ctrl.selectedShiftName = 'test';
    ctrl.refreshFilterResults();
    Integer countRecords1 = ctrl.allFilteredShift.size();

    ctrl.selectedShiftName = '';
    ctrl.refreshFilterResults();
    Integer countRecords2 = ctrl.allFilteredShift.size();

    ctrl.selectedShiftName = 'testShift2';
    ctrl.refreshFilterResults();
    Integer countRecords3 = ctrl.allFilteredShift.size();  

    Test.stopTest();

    System.assertEquals(3, countRecords1);
    System.assertEquals(3, countRecords2);
    System.assertEquals(2, countRecords3);
  }

  @isTest static void testSelectShift() {
      
    //Create multiple Shift records
    List<Shift__c> shiftList = new List<Shift__c>();
    
    for(integer shiftCnt = 0; shiftCnt < 10; shiftCnt++){
        shiftList.add(new Shift__c());
    }
    
    List<Database.SaveResult> shiftSaveResultList = Database.insert(shiftList);
    
    //Get the Ids of the inserted shift records
    Set<Id> shiftIdSet = new Set<Id>();
    
    for(Database.SaveResult saveResult :shiftSaveResultList){
        if(saveResult.isSuccess()){ shiftIdSet.add(saveResult.getId()); }
    }
    
    //Retrieve the newly created Shift records
    List<Shift__c> newShiftRecList = [select Id, Name From Shift__c Where Id IN :shiftIdSet];
  
    Shift__c contRepForInsert = new Shift__c();
      Database.SaveResult contRepSaveResultList = Database.insert(contRepForInsert);
      List<Shift__c> contRepForInsertList = [Select Id, Name From Shift__c where Id = :contRepSaveResultList.getId()];
  
      Test.setCurrentPage(Page.ContactReportCreate);
      ApexPages.CurrentPage().getparameters().put('pid', contRepForInsertList[0].Id);
      
      Test.startTest();
      
        ApexPages.StandardController sc = new 
        ApexPages.standardController(contRepForInsertList[0]);
        CheckInCreateCtrl ctrl = new CheckInCreateCtrl(sc);
        ctrl.refreshFilterResults();
        ApexPages.CurrentPage().getparameters().put('shiftName', 'testShift2');
      
      Test.stopTest();
  
      System.assertEquals(newShiftRecList[0].Id, ctrl.contactRep.Shift__c);

    }}