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
Ana RottaroAna Rottaro 

Test Class for a Class that should create new records of a custom object

I wrote a class that is set off through process builder if an Opportunity's stage field is closed won.  The class makes records called Ads (a custom onject) based on the opportunity line items (OLI).  Depending on the OLI code, the Ad is created with different information in the fields. This works in the sandbox, but when I write the test class I can't get it to pass because it says the list of Ads is empty. I decided to write the test class as only testing one OLI/Ad type called AWR first so that I could see if I am writing it correctly and then test all the cases.  Here's what I have: 

```@isTest
private class testCreateAd {
 @isTest static void testCreateAd() {
        // Add products and  price books
        List<sObject> lsprd = Test.loadData(Product2.sObjectType, 'testProducts');
        List<sObject> lspb = Test.loadData(Pricebook2.sObjectType, 'testPricebooks');
        
        // Assign standard price to each product and add it to the standard pricebook        
        for(sObject prd : lsprd) {
            PricebookEntry pbe = new PricebookEntry(UnitPrice = 0, Pricebook2Id = Test.getStandardPricebookId(), Product2Id = prd.Id, IsActive = TRUE);
            insert pbe;
        }
        
        // Add price book entries for each product in each price book
        List<sObject> lspbe = Test.loadData(PricebookEntry.sObjectType, 'testPricebookEntry');
        
        // Get the ID for the College price book
        List<Pricebook2> pb = [Select Id, Name From Pricebook2 Where Name = 'College Price Book'];
        System.debug(pb);
        Pricebook2 CPB = pb.get(0);
                
        // Insert account 
        Account acct = new Account(Name = 'Test Account');
        insert acct;
        
        // Add opportunity to the test account
        Opportunity opp = new Opportunity(Name = 'Opp', AccountId = acct.Id, CloseDate = system.today(), 
                                          StageName = 'Proposal', Start_Date__c = system.today(),
                                          Discount_Level__c = NULL, Pricebook2Id = CPB.Id, Type = 'New Business');
        insert opp;
        System.debug(opp);
 
     //Getting Date
        String d = String.valueOf(Date.today()).removeEnd(' 00:00:00');
     
        Test.startTest();
     
        // Choose a package to automatically add opportunity line items to the opportunity
        opp.Package__c = 'Reach Max';
        opp.Discount_Level__c = NULL;
        update opp;
        System.debug(opp);
 //Close the opp so that the process builder goes off to run the CreateAd class 
        opp.StageName = 'Closed Won';
        update opp;
        System.debug(opp); 
        Test.stopTest();

     //AWR coded opportunity line item and ad asserts
     List<Ad__c> adsAWR = [Select Name, Amount_Purchased__c, Account__c, 
                                                 End_Date__c, 
                                                GUID__c, Start_Date__c, Reporting_Type__c, 
                                                 Line_Item_Notes__c, OwnerId, Status__c, 
                                                 Type__c From Ad__c];
      Ad__c adAWR = adsAWR.get(0);
     
     List<OpportunityLineItem> olisAWR = [Select Name, Product2Id, Quantity, ProductCode, Subtotal, Description From OpportunityLineItem Where OpportunityId =: opp.Id];
        System.debug(olisAWR);
     OpportunityLineItem oliAWR = olisAWR.get(0);
     
     System.assert(adAWR.Name == acct.Name + ' ' + oliAWR.ProductCode + ' ' + d);
     System.assert(adAWR.Amount_Purchased__c == oliAWR.Subtotal);
     System.assert(adAWR.Account__c == acct.ID);
     System.assert(adAWR.End_Date__c == acct.Premium_Profile_End__c);
     System.assert(adAWR.GUID__c == acct.GUID__c);
     System.assert(adAWR.Start_Date__c == acct.Premium_Profile_Start__c);
     System.assert(adAWR.Reporting_Type__c == acct.Reporting_Type__c); 
     System.assert(adAWR.Line_Item_Notes__c == oliAWR.Description); 
     System.assert(adAWR.OwnerId == '0053600000DjFmXAAV');
     System.assert(adAWR.Status__c == 'Pending Live'); 
     System.assert(adAWR.Type__c == oliAWR.ProductCode);
 
    }
}```

Thanks in advance! I'm new to apex!