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
Dan WuenschDan Wuensch 

Advanced Apex Specialist - Error - Increase test coverage with unit tests

On Step 5 of Advanced Apex Specialist (Increase test coverage with unit tests), I am getting the following error when checking the challenge:

Challenge Not yet complete... here's what's wrong: 
Ensure that you create an instance of product2Extension.

Reviewing the logs seems to point to a problem in a different area than testing Product2Extension.

"Assertion Failed: Expected: 10, Actual: 20" - TestDataFactory.VerifyQuantityOrdered line 15

My unit tests that use VerifyQuantityOrdered as requested pass and are working as intended. My debug statements in TestDataFactory for setting up test data and in the unit test in OrderTests are not being logged out in this anonymous block execution, pointing to a direct invocation of TestDataFactory.VerifyQuantityOrdered directly by the Trailhead anonymous Apex, passing two Products and a qtyOrdered value of 20. The first Product passed has a Quanity_Ordered__c value of 0 and the updated product passed has a Quanity_Ordered__c value of 10. Since the qtyOrdered passed is 20, the expectation is:

public static void VerifyQuantityOrdered(Product2 originalProduct, Product2 updatedProduct, Integer qtyOrdered) {
     System.assertEquals(updatedProduct.Quantity_Ordered__c, (originalProduct.Quantity_Ordered__c + qtyOrdered));
}

0 + 20 is not equalling 10 as expected. It seems to me as though the anonymous block in this step is passing the wrong value for updatedProduct.Quantity_Ordered__c.

Any recommendations?
Best Answer chosen by Dan Wuensch
Dan WuenschDan Wuensch
As it turns out, the cause of the error message "Ensure that you create an instance of product2Extension" was that I was instantiating the extension controller in my unit test without passing a StandardController instance to the constructor.

All Answers

Zachery EngmanZachery Engman
Your method there for VerifyQuantityOrdered looks correct.

I would check your method, OrderUpdate_UnitTest, in OrderTest.  I assume you are calling that VerifyQuantityOrdered method from there, be sure you are re-querying and iterating over the updated Product2 records as you are calling that method so that the  Quantity_Ordered__c is not still 0.  
Dan WuenschDan Wuensch
As it turns out, the cause of the error message "Ensure that you create an instance of product2Extension" was that I was instantiating the extension controller in my unit test without passing a StandardController instance to the constructor.
This was selected as the best answer
Dan WuenschDan Wuensch
Hi Jeremiah! It looks like the problem is with the requirement "product family of each record incremented throughout the list".

Each time you are creating a Product, you loop through every product family and set the Family field. What ends up happening is each of your products has a Family value of the last picklist entry in that list. Instead, each Product you create should use the next Family value in the list:

First product created - first family picklist value
Second product created - second family picklist value
...

You can debug out each product's Family value before adding it to prodList to ensure that the Family cycles values each time. Beware of overrunning the buffer of available Family values when there is a larger number of Products being created than there are available Family values.

Hope this helps!
Shailendra Singh 69Shailendra Singh 69
Hi Dan, still getting the error. Can you share the code plz.
Quan CQuan C
Hi I am encountering this error at step 4: There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0 with id 0F90N0000007riASAQ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

I find out Id 0F90N0000007riASAQ is related to object CollaborationGroup.

Here are my codes, any idea?
 
public static CollaborationGroup ConstructCollaborationGroup() {
        CollaborationGroup cg = new CollaborationGroup(
            Name = 'TEST' + Constants.INVENTORY_ANNOUNCEMENTS,
            CollaborationType = 'Public');
        insert cg;
        return cg;
    }

public static void InsertTestData(Integer cnt) {        
        CollaborationGroup cg = ConstructCollaborationGroup();
        List<Product2> products = ConstructProducts(cnt);
        List<PricebookEntry> PricebookEntries = ConstructPricebookEntries(products);
        List<Account> accts = ConstructAccounts(cnt);
        List<Contact> contacts = ConstructContacts(cnt, accts);
        List<Order> orders = ConstructOrders(cnt, accts);
        List<OrderItem> orderItems = ConstructOrderItems(cnt, PricebookEntries, orders);
    }
Quan CQuan C
Someone helped me out, I need to remove insert statements from all methods, instead, insert them in InsertTestData() method.
SRINIZKUMAR  KONAKANCHISRINIZKUMAR KONAKANCHI
@Jeremaih - It is a badly worded requirement. Try this!
 
public static List<Product2> ConstructProducts(Integer cnt){
        List<Product2> prodList = new List<Product2>();
        Integer j = 0;
        for(Integer i=0;i<cnt;i++){
            Product2 prod = new Product2();
            prod.Name = 'Test ' +i;
            prod.isActive = true;
            prod.Initial_Inventory__c = 10;
            prod.Family = Constants.PRODUCT_FAMILY[j].getValue();
            j++;
            if(j>=Constants.PRODUCT_FAMILY.size()) j=0;
            prodList.add(prod);
        }
        return prodList;
    }

 
Quan CQuan C
@Jeremiah, hope this helps:
 
public static List<Product2> ConstructProducts(Integer cnt) {
    
    List<Product2> products = new List<Product2>();
    List<String> families = new List<String>();
    
    for (Schema.PicklistEntry p: Constants.PRODUCT_FAMILY) {
        families.add(p.getValue());
    }
    
    for (Integer i = 0; i < cnt; i++) {
        Product2 p = new Product2(
            Name = 'Product ' + i,
            IsActive = true,
            Initial_Inventory__c = 10,
            Family = i < families.size() ? families[i] : families[0]);
        
        products.add(p);
    }
    return products;
}
SRINIZKUMAR  KONAKANCHISRINIZKUMAR KONAKANCHI
@Jeremiah - Are you using the 'cnt' parameter of InsertTestData to pass to all methods as a parameter? See if your code is any different from this -
public static void InsertTestData(Integer cnt){
		CollaborationGroup grp = ConstructCollaborationGroup();
        Insert grp;
		List<Product2> productList = ConstructProducts(cnt);
		Insert productList;
        List<PricebookEntry> pricebookEntryList = ConstructPricebookEntries(productList);
        Insert pricebookEntryList;
		List<Account> accountList = ConstructAccounts(cnt);
        Insert accountList;
		List<Contact> contactList = ConstructContacts(cnt,accountList);
        Insert contactList;
		List<Order> orderList = ConstructOrders(cnt,accountList);
        Insert orderList;
		List<OrderItem> orderItemsList = ConstructOrderItems(cnt,pricebookEntryList,orderList);
        Insert orderItemsList;
    }

 
Maanas DesaiMaanas Desai
Hi ,
 
I have completed the Multiple choice questions for Platform Developer 2 as well as the 4 super badges. How do i link Webaccessor account to Trailhead? Has anyone got any email from salesforce after they completed the 4 super badges?
User-added image
Quan CQuan C
@Maanas Desai cognizant, you will receive an email asking you to link your trailhead account and Webaccessor within a week (I reiceved it 3 days after I completing 4 super badges). Here is the detailed article about the process: http://certification.force.com/pkb/articles/Public_KB/Link-Your-Trailhead-and-Webassessor-Accounts

Hope this helps, also I am preparing for Platform Developer 2 multiple choice questions, any tips for prepartion? Thanks.