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
KRayKRay 

test class Error System.QueryException: List has no rows for assignment to SObject

Hi everyone, I'm having trouble with a test class. I'm passing a value to the target class variable but when the SOQL query executes, it returns "System.QueryException: List has no rows for assignment to SObject". Please help


Class
global class myClass{
public String AccountName{get;set;}
public Account accId{get;set;}
public String CNum{get;set;}

public VarsityAppInvoice(ApexPages.StandardController cAccount){         accId = (Account)cAccount.getRecord();         CNum = [Select Customer_Number__c FROM Account where id=:accId.id].Customer_Number__c;     }


   public Boolean getAccountStatus(){
     AccountName= [SELECT Name FROM Account WHERE Customer_Number__c=:CNum].Name;
     if(AccountName=null){
        return false;
     } else {
        return true
     }
   }

}

TestClass
@isTest
global class myClassTest{
static myClass con = new myClass();

public myClass(){
con.CNum='123456';
}

    @isTest static void testGetAccountStatus(){
            Boolean results = con.getAccountStatus();
            System.Debug(results);
     }

}

Even if I update con.CNum in the "testGetAccountStatus" method, I get the same error back, when the test runs. "System.QueryException: List has no rows for assignment to SObject"


 
Best Answer chosen by KRay
Mudasir WaniMudasir Wani

Hello KRay,

You have two options as suggested by BalajiRanganathan

You can use following code.

Please check your class code codition on line 11 it should be double equal sign.

@isTest 
global class myClassTest{ 
      
@isTest static void positiveTestGetAccountStatus(){ 
     myClass con = new myClass();
     //creating account 
     Account acc = new Account();
     acc.Customer_Number__c = 123456;
     acc.Name = 'TestAccount';
     insert acc;
     Boolean results = con.getAccountStatus(); 
     System.Debug(results);

@isTest static void negativeTestGetAccountStatus(){ 
     myClass con = new myClass();
     //creating account 
     Account acc = new Account();
     acc.Customer_Number__c = null;
     acc.Name = 'TestAccount';
     insert acc;
     Boolean results = con.getAccountStatus(); 
     System.Debug(results);

}

 

All Answers

BalajiRanganathanBalajiRanganathan
You need to insert your test data in your test method or you need to use @isTest(SeeAllData=true)
Mudasir WaniMudasir Wani

Hello KRay,

You have two options as suggested by BalajiRanganathan

You can use following code.

Please check your class code codition on line 11 it should be double equal sign.

@isTest 
global class myClassTest{ 
      
@isTest static void positiveTestGetAccountStatus(){ 
     myClass con = new myClass();
     //creating account 
     Account acc = new Account();
     acc.Customer_Number__c = 123456;
     acc.Name = 'TestAccount';
     insert acc;
     Boolean results = con.getAccountStatus(); 
     System.Debug(results);

@isTest static void negativeTestGetAccountStatus(){ 
     myClass con = new myClass();
     //creating account 
     Account acc = new Account();
     acc.Customer_Number__c = null;
     acc.Name = 'TestAccount';
     insert acc;
     Boolean results = con.getAccountStatus(); 
     System.Debug(results);

}

 

This was selected as the best answer
KRayKRay
Guys, I assumed that SF Test would grab the records but that's what I get for not reading. I used @testSetup to call a Utility class that creates the test records and passes it back to the test class above, Works like a charm. Thanks Everyone
Divya Krishnan 1Divya Krishnan 1
Help to write test class for the below apex class
@RestResource(urlMapping='/Oppty/*')
global with sharing class OpportunitySFDCService{


    @HttpPost
    global static void UpdateOppMASCustomerID(String MASCustomerID, String OrderSFDCID) {
      
       Order__c orderOppID=[Select Id,Opportunity__c from Order__c where  Order_SFDC_ID__c=:OrderSFDCID];
       Opportunity  opp=new Opportunity(Id=orderOppID.Opportunity__c,MASUniqueId__c=MASCustomerID);
       update opp;
       
    }   

}