+ Start a Discussion
Suresh RaghuramSuresh Raghuram 

Advanced Apex Specialist challenge 5 and running into the following error.

Challenge Not yet complete... here's what's wrong: 
Ensure that test methods can't use live data.

@isTest (seeAllData=false)
private class Product2Tests {
@testSetup
public static void SetupTestData(){  
    
    CollaborationGroup ChatterGroup = new CollaborationGroup(
        Name = 'TEST'+constants.INVENTORY_ANNOUNCEMENTS,  
        CollaborationType = 'Public',
        CanHaveGuests = false,
        IsArchived = false,
        IsAutoArchiveDisabled = true
        );
    insert ChatterGroup;
     
}
    
@isTest
    static void Product2Extension_UnitTest(){
        PageReference pageRef = Page.Product2New;
        Test.setCurrentPage(pageRef);
        Product2 prod = new Product2(name='Test',isActive=true);        
        ApexPages.StandardController stdcontroller = new 
        ApexPages.StandardController(prod);        
        Product2Extension ext = new Product2Extension(stdcontroller);        
         System.assertEquals(Constants.DEFAULT_ROWS, ext.productsToInsert.size());
        
        ext.addRows();
        System.assertEquals(Constants.DEFAULT_ROWS * 2, 
        ext.productsToInsert.size());
        
        for (Integer i = 0; i < 5; i++) {
            Product2Extension.ProductWrapper wrapper = ext.productsToInsert[i];
            
            Product2 testProd = new Product2();
            testProd.Name = 'Test Product ' + i;
            testProd.IsActive = true;
            testProd.Initial_Inventory__c = 20;
            testProd.Family = Constants.PRODUCT_FAMILY[0].getValue();
            wrapper.productRecord = testProd;
            
            PricebookEntry testPBEntry = new PricebookEntry();
            testPBEntry.IsActive = true;
            testPBEntry.UnitPrice = 10;
            wrapper.pricebookEntryRecord = testPBEntry;
        }
        
        Test.startTest();
        ext.save();
        Test.stopTest();
        
        ext.GetFamilyOptions();
        ext.GetInventory();
        List<Product2> createdProducts = [
            SELECT
              Id
            FROM
               Product2
        ];
        System.assertEquals(5, createdProducts.size());
    }
    
   @isTest
    static void Product2Trigger_UnitTest(){
        TestDataFactory.InsertTestData(5); 
          test.startTest();
        
     
        Order rec = [select id, Status from Order limit 1];
        Product2 prod = [SELECT Family,Id,Name,Quantity_Ordered__c,Quantity_Remaining__c FROM  Product2 WHERE Name LIKE :'Test Product%' limit 1];
   
       rec.status = constants.ACTIVATED_ORDER_STATUS;      
       Update rec;
       Product2 updatedprod = [SELECT Family,Id,Name,Quantity_Ordered__c,Quantity_Remaining__c FROM Product2 limit 1];
        
  TestDataFactory.VerifyQuantityOrdered(prod,updatedprod,constants.DEFAULT_ROWS);
       Test.stopTest();
    
    }
}

***********************
/**
 * @name TestDataFactory
 * @description Contains methods to construct and/or validate commonly used records
**/
public with sharing class TestDataFactory {

    /**
     * @name ConstructCollaborationGroup
     * @description
    **/
    public static CollaborationGroup ConstructCollaborationGroup(){
        //ToDo: Ensure this method returns a single Chatter CollaborationGroup
        //    whose Name starts with 'TEST' followed by the INVENTORY_ANNOUNCEMENTS constant
        //    and configured so anyone can join, see and post updates.
        CollaborationGroup grp = new CollaborationGroup();
        
        grp.Name='TEST'+Constants.INVENTORY_ANNOUNCEMENTS;
        grp.CollaborationType='Public';
        grp.IsAutoArchiveDisabled = true;
        return grp;
    }

    /**
     * @name CreateProducts
     * @description Constructs a list of Product2 records for unit tests
    **/
    public static List<Product2> ConstructProducts(Integer cnt){
        //ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records
        //  with all the required fields populated
        //  and IsActive = true
        //  an Initial Inventory set to 10
        //  and iterating through the product family picklist values throughout the list.
        List<Schema.PicklistEntry> familyLst = Constants.PRODUCT_FAMILY;
        List<Product2> testPrdts = new List<Product2>();
        for(Integer i=0;i<cnt;i++){
            Integer index = Math.mod(i, familyLst.size());
            testPrdts.add(new Product2(Name='TEST_PRODUCT_'+i,isActive=TRUE,Initial_Inventory__c=10,Family=familyLst[index].getValue()));
        }
       return testPrdts;
    }

    /**
     * @name CreatePricebookEntries
     * @description Constructs a list of PricebookEntry records for unit tests
    **/
    public static List<PricebookEntry> ConstructPricebookEntries(List<Product2> prods){
        //ToDo: Ensure this method returns a corresponding list of PricebookEntries records
        //  related to the provided Products
        //  with all the required fields populated
        //  and IsActive = true
        //  and belonging to the standard Pricebook
      List<PricebookEntry> testPricebookEntries = new List<PricebookEntry>();
        for(Product2 prod:prods){
            testPricebookEntries.add(new PricebookEntry(Product2Id=prod.Id,PriceBook2Id=Constants.STANDARD_PRICEBOOK_ID,IsActive=TRUE,UnitPrice=5));
        }
        return testPricebookEntries;
    }

    /**
     * @name CreateAccounts
     * @description Constructs a list of Account records for unit tests
    **/
    public static List<Account> ConstructAccounts(Integer cnt){
        //ToDo: Ensure this method returns a list of size cnt of uniquely named Account records
        //  with all of the required fields populated.
        List<Account> testAcc = new List<Account>();
        for(Integer i=1;i<=cnt;i++){
            testAcc.add(new Account(Name='TEST_ACCOUNT_'+i));
        }
        return testAcc;
    }
    /**
     * @name CreateContacts
     * @description Constructs a list of Contacxt records for unit tests
    **/
    public static List<Contact> ConstructContacts(Integer cnt, List<Account> accts){
        //ToDo: Ensure this method returns a list, of size cnt, of uniquely named Contact records
        //  related to the provided Accounts
        //  with all of the required fields populated.
        List<Contact> testCnts = new List<Contact>();
        for(Integer i=0;i<cnt;i++){
            Integer index = Math.mod(i, accts.size());
            testCnts.add(new Contact(LastName='TEST_CONTACT_'+i,AccountId=accts[index].Id));
        }
        return testCnts;
    }

    /**
     * @name CreateOrders
     * @description Constructs a list of Order records for unit tests
    **/
    public static List<Order> ConstructOrders(Integer cnt, List<Account> accts){
        //ToDo: Ensure this method returns a list of size cnt of uniquely named Order records
        //  related to the provided Accounts
        //  with all of the required fields populated.
        List<Order> testOrds = new List<Order>();
        for(Integer i=0;i<cnt;i++){
            Integer index = Math.mod(i, accts.size());
            testOrds.add(new Order(PriceBook2Id=Constants.STANDARD_PRICEBOOK_ID,Name='TEST_ORDER'+i,AccountId=accts[index].Id,EffectiveDate=System.today()-10,Status='Draft'));
        }
        return testOrds;
    }

    /**
     * @name CreateOrderItems
     * @description Constructs a list of OrderItem records for unit tests
    **/
    public static List<OrderItem> ConstructOrderItems(integer cnt, list<pricebookentry> pbes, list<order> ords){
        //ToDo: Ensure this method returns a list of size cnt of OrderItem records
        //  related to the provided Pricebook Entries
        //  and related to the provided Orders
        //  with all of the required fields populated.
        //  Hint: Use the DEFAULT_ROWS constant for Quantity as it will be used in the next challenge
        List<OrderItem> testOrdItms = new List<OrderItem>();
        for(Integer i=0;i<cnt;i++){
            Integer indexP = Math.mod(i, pbes.size());
            Integer indexO = Math.mod(i, ords.size());
            testOrdItms.add(new OrderItem(PricebookEntryId=pbes[indexP].Id,OrderId=ords[indexO].Id,Quantity=Constants.DEFAULT_ROWS,UnitPrice=10));
        }        
        return testOrdItms;
    }

    /**
     * @name SetupTestData
     * @description Inserts accounts, contacts, Products, PricebookEntries, Orders, and OrderItems.
    **/
    public static void InsertTestData(Integer cnt){
        //ToDo: Ensure this method calls each of the construct methods
        //  and inserts the results for use as test data.
        List<Account> acts = ConstructAccounts(cnt);
        insert acts;
        List<Contact> cnts = ConstructContacts(cnt, acts);
        insert cnts;
        List<Product2> prds = ConstructProducts(cnt);
        insert prds;
        List<PricebookEntry> prcbookEntries = ConstructPricebookEntries(prds);
        insert prcbookEntries;
        List<Order> ords = ConstructOrders(cnt,acts);
        insert ords;
        List<OrderItem> ordItems = ConstructOrderItems(cnt,prcbookEntries,ords);
        insert ordItems;
    }
    
    public static void VerifyQuantityOrdered(Product2 originalProduct, Product2 updatedProduct, Integer qtyOrdered){        
        System.assertEquals(originalProduct.Quantity_Ordered__c+qtyOrdered,updatedProduct.Quantity_Ordered__c);
    }

}
 
SandhyaSandhya (Salesforce Developers) 
Hi,

Refer below link for similar discussion.

https://developer.salesforce.com/forums/?id=9060G0000005OGJQA2
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya