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
Andy StollmeyerAndy Stollmeyer 

VF Test Class Not Finding Account Object

I've created a VF page that works great.  However, I cannot get the test class to pass for the life of me.  Below is an exceprt from my VF extension:
public class OpportunityWonExt{
    
    ApexPages.StandardController controller;
    public Opportunity theOpp {get;set;}
    public Account act {get; set;}
    public Contract ct {get; set;}
    
    public OpportunityWonExt(ApexPages.standardController controller){
        theOpp = (Opportunity)controller.getRecord();
        theOpp.CloseDate = Date.today();
        theOpp.StageName = 'Closed Won';
        String actid = theOpp.AccountID;
        OpportunityWonProcess__c settings = OpportunityWonProcess__c.getInstance('Standard Settings');
        String ctOwner = settings.Contract_Owner__c;
        act = [SELECT Id, Name, Company_Size_Type__c FROM Account WHERE ID =:actid ];
        system.debug('LOOK HERE act' + act);
        ct = new Contract();
        ct.AccountID = theOpp.AccountID;
        ct.Status = 'Draft';
        ct.ContractTerm = 12;
        ct.OwnerID = ctOwner;
Below is an excerpt from my test class
 
@isTest(SeeAllData=true) 
private Class OpportunityWonTest{
    
    static testMethod void theTests(){
        // setup some variables 
        String standardPriceBookId = '';
        String UID = '';
        String opportunityName = 'This Is My Favorite Opportunity1234';
        User u = [SELECT ID FROM User WHERE IsActive = TRUE LIMIT 1];
        UID = u.ID;
        
        // Select pricebook
        PriceBook2 pb2Standard = [SELECT ID FROM Pricebook2 WHERE IsStandard = TRUE];
        standardPriceBookId = pb2Standard.Id;
        
        // Setup Account
        Account a = new Account(Name = 'Test Account',Website = 'www.test.com',Company_Size_Type__c = 'Enterprise',OwnerID=UID);
        insert a;
        
        // Setup Opportunity
        Date closedate =  Date.today();
        Opportunity o = new Opportunity(Account = a, Name=opportunityName, StageName='Prospecting', CloseDate=Date.today(),Logo_Use__c = 'Yes', OwnerID=UID);
        insert o;
        Opportunity opp = [Select ID from Opportunity WHERE Name = 'This Is My Favorite Opportunity1234'];
        
        // set up product2
        Product2 p2 = new Product2(Name='Test Product',isActive=true);
        insert p2;
        
        // set up PricebookEntry
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true);
        insert pbe;
        
        // set up OpportunityLineItem
        OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id, Quantity=1, TotalPrice=99, Discount__c=0, Contract_Length__c = 12);
        insert oli;

        // load the page       
        PageReference pageRef = Page.OpportunityWon;
        pageRef.getParameters().put('Id',opp.id);
        Test.setCurrentPageReference(pageRef);
        
        // load the extension
        ApexPages.StandardController newcontroller = new ApexPages.StandardController(opp);
       OpportunityWonExt OppWonExt = new OpportunityWonExt(newcontroller);

When I run the test I get the error "System.QueryException: List has no rows for assignment to SObject" pointing to line 15 for the extension class and line 45 for the test class.

The confusing part is the Account is defined, as it saves the Account fields when I save the page.  Additionally, when I debug there is an Account being selected, so no idea how the list is ending up empty...
Best Answer chosen by Andy Stollmeyer
scottbcovertscottbcovert

Hi Andy,

Try breaking out the AccountId field initialization from line 24 in your test class to the line below like so:

 

Opportunity o = new Opportunity(Name=opportunityName, StageName='Prospecting', CloseDate=Date.today(),OwnerID=UID);
o.AccountId = a.Id;

Let me know if that works.

Best,
Scott

All Answers

scottbcovertscottbcovert
Hi Andy,

The SOQL query on line 24 of your test class only pulls in the Id value of the test opportunity record but on line 12 of the controller you try to use its AccountId field. This field will be null since the opportunity SOQL query did not search for it meaning on line 15 you get no result from the Account SOQL query. Try changing line 24 of your test class to the following:
 
Opportunity opp = [Select ID, AccountId from Opportunity WHERE Name = 'This Is My Favorite Opportunity1234'];

Hope that helps,
Scott
Andy StollmeyerAndy Stollmeyer
@scottbcovert, first off thanks for the reply.  Unfortunately that didn't fix it, same error on the same lines.  
scottbcovertscottbcovert

Hi Andy,

Try breaking out the AccountId field initialization from line 24 in your test class to the line below like so:

 

Opportunity o = new Opportunity(Name=opportunityName, StageName='Prospecting', CloseDate=Date.today(),OwnerID=UID);
o.AccountId = a.Id;

Let me know if that works.

Best,
Scott
This was selected as the best answer
Andy StollmeyerAndy Stollmeyer
That was it!  Very odd that you need to set the ID outside..actually had to do this a second time within the same test class after rerunning.  

Thanks for the help!
scottbcovertscottbcovert
Great; happy to help!