+ 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