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
Mike@MungosMike@Mungos 

Struggling to correct a Test Class Error

Hi 

I am attempting to write a Test Class for a trigger I wrote.  However, I keep getting the following errors, "TriggerTest2.validateUpdateBedspaceWithNewNomTen(), Details: System.QueryException: List has no rows for assignment to SObject Class.TriggerTest2.validateUpdateBedspaceWithNewNomTen: line 6, column 1" and  "System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.TriggerTest2.validateUpdateBedspaceWithNewNomTen: line 6, column 1".  Can anyone help me to figure out where I've gone wrong?

@isTest
Private class TriggerTest2 {
   
    static testMethod void validateUpdateBedspaceWithNewNomTen() {
     
      String rtBed = [SELECT Id FROM RecordType WHERE DeveloperName = 'Bedspace' AND SobjectType = 'Bedspace__c' LIMIT 1].Id;
      Bedspace__c ref = new Bedspace__c(Scheme__c = 'a052000000Hcpg2', RecordTypeId = rtBed); 
    insert ref;
     
      ref = [SELECT Id, Current_nomination_tenancy__c FROM Bedspace__c WHERE Id =:ref.Id];
     
      String rtNom = [SELECT Id FROM RecordType WHERE DeveloperName = 'Nomination_Tenancy' AND SobjectType = 'Nomination_Tenancy__c' LIMIT 1].Id;
      Nomination_Tenancy__c ra = new Nomination_Tenancy__c(Bedspace__c = ref.Id, Client__c = 'a052000000Hcpg2', RecordTypeId = rtNom); 
    insert ra;
   
    ref = [SELECT Id, Current_nomination_tenancy__c FROM Bedspace__c WHERE Id =:ref.Id];
   
    System.assertEquals(ref.Current_nomination_tenancy__c, ra.Id);
    } 

}

Many Thanks,

Mike
Oliver RixOliver Rix
Hi Mike,

This is a good question; I have been scratching my head over a similar issue and look forward to seeing if someone is able to help you.

Regards,
Oliver
Anoop yadavAnoop yadav
Hi,

You should use @isTest(seealldata=true)

One more thing, you should not depend on the salesforce data.
You should create your own data in test Class.

For more information see the below link.
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Kunal BindalKunal Bindal
This can help you
@isTest(seealldata=false)
public class Test {
 static testMethod void test1() {
    
    
    Schema.DescribeSObjectResult cfrSchema = Schema.SObjectType.Contact; 
Map<String,Schema.RecordTypeInfo> ContactRecordTypeInfo = cfrSchema.getRecordTypeInfosByName(); 
Id rtId = ContactRecordTypeInfo.get('Master').getRecordTypeId();
     System.debug(rtId);
     //System.assertEquals(null, rtId);
 }
}

ManjunathManjunath
Hi ,

Check if your "DeveloperName" is correct. What you have written is right. Try removing that and give Name instead.

I dont think you need @isTest(seealldata=false)

Regards,
Roy LuoRoy Luo
Don't reuse the variable name. Change
 ref = [SELECT Id, Current_nomination_tenancy__c FROM Bedspace__c WHERE Id =:ref.Id];
to
Bedspace__c  dbRef =  [SELECT Id, Current_nomination_tenancy__c FROM Bedspace__c WHERE Id =:ref.Id];

I believe I have seen this kind of issue before and it took me a long time to track it down, though I still don't know why that would be an issue.
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Mike,

Please try below code :-
@isTest
Private class TriggerTest2 
{
   
    static testMethod void validateUpdateBedspaceWithNewNomTen() 
    {
      Scheme__c SchemeObj = new Scheme__c();
      // SchemeObj.Name='testDate';
      // Add all mendatory field here for Scheme__c object
      insert SchemeObj;

      String rtBed = [SELECT Id FROM RecordType WHERE DeveloperName = 'Bedspace' AND SobjectType = 'Bedspace__c' LIMIT 1].Id;
      Bedspace__c ref = new Bedspace__c(Scheme__c = SchemeObj.id, RecordTypeId = rtBed); 
      insert ref;
        
      Client__c ClientObj = new Client__c();
      // ClientObj.name ='Test client';
      // Add all mendatory field here for Client__c object
      insert ClientObj;

      String rtNom = [SELECT Id FROM RecordType WHERE DeveloperName = 'Nomination_Tenancy' AND SobjectType = 'Nomination_Tenancy__c' LIMIT 1].Id;
      Nomination_Tenancy__c ra = new Nomination_Tenancy__c(Bedspace__c = ref.Id, Client__c = ClientObj.id, RecordTypeId = rtNom); 
      insert ra;
   
      ref = [SELECT Id, Current_nomination_tenancy__c FROM Bedspace__c WHERE Id =:ref.Id];
   
      //System.assertEquals(ref.Current_nomination_tenancy__c, ra.Id);
    } 

}


PS: if this answers your question then hit Like and mark it as solution!