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
Sonam PatilSonam Patil 

Apex codeTest class Exception

Hi,
Below is my code for the test class.
I am getting this Exception,
System.sObjectException:sObject row was retrieved via SOQL without querying the required field:Book__c.Price__c.
 
Please, can anyone help me?

@isTest
public class BookTest {
    public static @isTest void btest(){
        Book__c b = new Book__c(Name='Kcloud', 
                                price__c=100);
        System.debug('The price of a book before insert is:'+b.Price__c);
        insert b;
        
        b=[Select id,Name from Book__c where id=:b.id];
        System.debug('The price of the book after thr trigger is fired:'+b.Price__c);
        System.assertEquals(90,b.price__c);
    }  

}​
 
 
Ajay K DubediAjay K Dubedi
Hi Sonam,
This is because you are using price__c without querying. Try this code :
@isTest
public class BookTest {
    public static @isTest void btest(){
        Book__c b = new Book__c(Name='Kcloud', 
                                price__c=100);
        System.debug('The price of a book before insert is:'+b.Price__c);
        insert b;
        
        b=[Select id,Name,price__c from Book__c where id=:b.id];
        System.debug('The price of the book after thr trigger is fired:'+b.Price__c);
        System.assertEquals(90,b.price__c);
    }  

}​
Regards,
Ajay
 
Sonam PatilSonam Patil
Thanks, Ajay.
But, now I am getting this error:
System.AssertExceptin:AssertionFailed