+ Start a Discussion
CloudNerdCloudNerd 

Error in Apex Test Class

I am trying to write a test class that inserts a new Opportunity and then a new custom object "Project" and then relates the opportunity to the Project.

I keep getting the following error in the code below when running the test:

"System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: MPM4_BASE__Milestone1_Project__c.Opportunity__r"
 
@isTest
private class newMilestoneTestClass {
    static testMethod void validatenewMilestone() {
       Opportunity o = new Opportunity(Name='Test Opp', CloseDate=date.today(), StageName='Closed Won');
       // Insert opportunity
       insert o;
        
       MPM4_BASE__Milestone1_Project__c p = new MPM4_BASE__Milestone1_Project__c(Name='Test Project', MPM4_BASE__Status__c='Set', Opportunity__c=o.Id);
       System.debug('Status before inserting new project: ' + p.MPM4_BASE__Status__c);
       System.debug('Opp is: ' + p.Opportunity__c);

       // Insert project
       insert p;
    
       // Retrieve the new project
       p = [SELECT MPM4_BASE__Status__c FROM MPM4_BASE__Milestone1_Project__c WHERE Id = :p.Id];

       // Test that the trigger correctly updated the related Opportunity
       System.assertEquals(o.Id, p.Opportunity__r.Id);
    }
    
}

 
Best Answer chosen by CloudNerd
Abhishek BansalAbhishek Bansal
Hi,

The API name of Opportunity field on Project is Opportunity__c so you have to query this field and use the same in assert statement.
Please update your test class as below :
 
@isTest
private class newMilestoneTestClass {
    static testMethod void validatenewMilestone() {
       Opportunity o = new Opportunity(Name='Test Opp', CloseDate=date.today(), StageName='Closed Won');
       // Insert opportunity
       insert o;
        
       MPM4_BASE__Milestone1_Project__c p = new MPM4_BASE__Milestone1_Project__c(Name='Test Project', MPM4_BASE__Status__c='Set', Opportunity__c=o.Id);
       System.debug('Status before inserting new project: ' + p.MPM4_BASE__Status__c);
       System.debug('Opp is: ' + p.Opportunity__c);

       // Insert project
       insert p;
    
       // Retrieve the new project
       p = [SELECT MPM4_BASE__Status__c,Opportunity__c FROM MPM4_BASE__Milestone1_Project__c WHERE Id = :p.Id];

       // Test that the trigger correctly updated the related Opportunity
       System.assertEquals(o.Id, p.Opportunity__c);
    }
    
}

Let me know if you have any issue.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

The API name of Opportunity field on Project is Opportunity__c so you have to query this field and use the same in assert statement.
Please update your test class as below :
 
@isTest
private class newMilestoneTestClass {
    static testMethod void validatenewMilestone() {
       Opportunity o = new Opportunity(Name='Test Opp', CloseDate=date.today(), StageName='Closed Won');
       // Insert opportunity
       insert o;
        
       MPM4_BASE__Milestone1_Project__c p = new MPM4_BASE__Milestone1_Project__c(Name='Test Project', MPM4_BASE__Status__c='Set', Opportunity__c=o.Id);
       System.debug('Status before inserting new project: ' + p.MPM4_BASE__Status__c);
       System.debug('Opp is: ' + p.Opportunity__c);

       // Insert project
       insert p;
    
       // Retrieve the new project
       p = [SELECT MPM4_BASE__Status__c,Opportunity__c FROM MPM4_BASE__Milestone1_Project__c WHERE Id = :p.Id];

       // Test that the trigger correctly updated the related Opportunity
       System.assertEquals(o.Id, p.Opportunity__c);
    }
    
}

Let me know if you have any issue.

Thanks,
Abhishek
This was selected as the best answer
CloudNerdCloudNerd
That worked perfect. Thanks Abhishek!