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
kathybbkathybb 

Please help me with Test Class

I'm trying to write a version of the Requisitions App from AppExchange.  It involves 3 custom objects (Purchase Items, Purchase Requisitions and Purchase Orders.)  I'm trying to write the part that updates certain Purchase Requisitions with the PO Number that they are ordered on.  I'm having trouble initializing my Test of the Extension code.  What I've got so far is:

public class Test_AttachPRTOPOController{
    static testmethod void Test_Attachment_process() {
        SFDC_Purchase_Order__c myPO = new SFDC_Purchase_Order__c();
       
		ApexPages.StandardController sc = new ApexPages.standardController(myPO);
        AttachPRTOPOController myPageCon = new AttachPRTOPOController(sc);

        List<SFDC_Purchase_Items__c> items = new List<SFDC_Purchase_Items__c>();
        string q1 = 'SELECT Id,Name FROM SFDC_Purchase_Items__c limit 1';
        items = database.query(q1);
        system.debug(q1);
        system.debug(items.size());
        if(items != null){
            for(SFDC_Purchase_Items__c thisItem : items){
                thisItem = (SFDC_Purchase_Items__c)thisItem;
                system.debug(thisItem.name + ' was created');
                system.assertNotEquals(thisItem,Null);
            }
        }                                          
            //create purchase requisitions
    }       
        
}

 I'm obviously doing something wrong because the query for Purchase_Items__c is returning null.  I can't really proceed with creating the rest of my test data until I can get hold of some Item records, and this is driving me crazy.  I can attach the Extension Code and pages, if needed, but it is rather long.

 

Thanks in advance for any insight.

 

Kathy (obvous Apex novice)

 

Blake TanonBlake Tanon

I'm not sure what the page controller is doing, but I think that when you do a new record you need to add some field values and then do an insert.  Is that happening in the pages controller?

kathybbkathybb
public class Test_AttachPRTOPOController{
    static testmethod void Test_Attachment_process() {
        SFDC_Purchase_Order__c myPO = new SFDC_Purchase_Order__c(status__c='New');
        database.insert(myPO);
		ApexPages.StandardController sc = new ApexPages.standardController(myPO);
        AttachPRTOPOController myPageCon = new AttachPRTOPOController(sc);

        List<SFDC_Purchase_Items__c> items = new List<SFDC_Purchase_Items__c>();
        string q1 = 'SELECT Id,Name FROM SFDC_Purchase_Items__c limit 1';
        items = database.query(q1);
        system.debug(q1);
        system.debug(items.size());
        if(items != null){
            for(SFDC_Purchase_Items__c thisItem : items){
                thisItem = (SFDC_Purchase_Items__c)thisItem;
                system.debug(thisItem.name + ' was created');
                system.assertNotEquals(thisItem,Null);
            }
        }                                          
            //create purchase requisitions
    }       
        
}

 

Hi, Blake,

 

I did as you suggested and inserted a Purchase Order.  The query is still returning nulls.  If I run it in the query editor or in the workbench, it returns 10 records as expected. I appreciate your answering a lot -- please let me know if another issue occurs to you.

 

Thanks,

Kathy

ForcepowerForcepower
Kathy, When you insert a new Purchase Order, is that supposed create some purchase items (the line items in the order?). I'm wondering if you have any items in your PO that you're inserting.
SFDC_Purchase_Order__c myPO = new SFDC_Purchase_Order__c(status__c='New');
database.insert(myPO);

You could either try creating SFDC_Purchase_Items__c object records thru' supplying the right data on the creation of a new SFDC_Purchase_Order__c or
just create a SFDC_Purchase_Items__c record explicitly (similar to how you did the PO).
Ram