+ Start a Discussion
Santosh Reddy MaddhuriSantosh Reddy Maddhuri 

Advanced Apex Specialist Superbadge - Stpe 5 Issue

Hi All,

Am facing this issue on step 5 , can any one help me complete this challenge.

User-added image
Best Answer chosen by Santosh Reddy Maddhuri
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
For benefit of others here is the solution.

1. Ensure that you declare following code snippet to overcome the above error
 
public Product2Extension(ApexPages.StandardController controller) { 
       productsToInsert = new List<ProductWrapper>();
       addRows();  
    }

2. If the above code snippet did not solve, then mostly its the trailhead test data not able to validat your code.For this create another trailhead playground and copy the code of all the 5 steps and re validate the step 5.This will pass the step.

Hope this helps!

All Answers

Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Here is the error
Challenge Not yet complete... here's what's wrong: 
Ensure you verify that the constructor sets the initial size of the productsToInsert list properly.
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
For benefit of others here is the solution.

1. Ensure that you declare following code snippet to overcome the above error
 
public Product2Extension(ApexPages.StandardController controller) { 
       productsToInsert = new List<ProductWrapper>();
       addRows();  
    }

2. If the above code snippet did not solve, then mostly its the trailhead test data not able to validat your code.For this create another trailhead playground and copy the code of all the 5 steps and re validate the step 5.This will pass the step.

Hope this helps!
This was selected as the best answer
Cody Maines DevCody Maines Dev
I ended up solving this error by changing my assertions. When I used System.assertEquals(controller.productsToInsert.size(),  Constants.DEFAULT_ROWS) it would not work. I changed it to System.assert(controller.productsToInsert.size() == Constants.DEFAULT_ROWS) it finally passed.
Raja Vivekanandhan 8Raja Vivekanandhan 8
I have the constructor as mentioned.
I event tried creating another play ground and have the same assertions mentioned above. 
I still get the error "Ensure you verify that the constructor sets the initial size of the productsToInsert list properly.".
Can any one help on resolving this issue? Thanks!
Preetham 345 SFDCPreetham 345 SFDC
Hi Ravi , 

I have done both the steps provided by you , but still i am not able to proceed ahead ,getting the same error upon click of check challenge 

Challenge Not yet complete... here's what's wrong: 
Ensure you verify that the constructor sets the initial size of the productsToInsert list properly.

I belive a anonymous script upon click of Check challenge button , I have debugged that as well

I still dont understand how 20 value is hardcoded  in the script up on click of Check Challenge button , Is this superbadge trail head issue ??

Execute Anonymous: Integer qtyOrdered = 10;
Execute Anonymous: Product2 originalProduct = new Product2(Quantity_Ordered__c = 0);
Execute Anonymous: Product2 updatedProduct = new Product2(Quantity_Ordered__c = qtyOrdered);
Execute Anonymous: testDataFactory.verifyQuantityOrdered( originalProduct, updatedProduct, 20 );
20:10:45.6 (6924719)|USER_INFO|[EXTERNAL]|0056A000001WgB7|developerpre@apex.com|Pacific Standard Time|GMT-07:00 20:10:45.6 (6962427)|EXECUTION_STARTED 20:10:45.6 (6968658)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex 20:10:45.6 (8132146)|SYSTEM_MODE_ENTER|false 20:10:45.6 (10455034)|SOQL_EXECUTE_BEGIN|[9]|Aggregations:0|SELECT id FROM PriceBook2 WHERE isStandard = TRUE LIMIT 1 20:10:45.6 (14239644)|SOQL_EXECUTE_END|[9]|Rows:1 20:10:45.6 (14751276)|USER_DEBUG|

[140]|DEBUG|@@@@@@@@@@@@@@@@@originalProduct.Quantity_Ordered__c  0 [141]|DEBUG|@@@@@@@@@@@@@@@@@updatedProduct.Quantity_Ordered__c  10 [142]|DEBUG|@@@@@@@@@@@@@@@@@qtyOrdered  20
[143]|System.AssertException: Assertion Failed: Expected: 20, Actual: 10


Please kindly some help , it has been two days appoximately i have been fighting on this , thanks in advance .



Thanks 
Preetam
Preetham 345 SFDCPreetham 345 SFDC
Hi Ravi , 

I figured it out , below link helped me 

https://developer.salesforce.com/forums/?id=9060G0000005OGJQA2

Thanks 
Preetam
AshesAshes
I am able to solve this issue by adding System.assert(pe.productsToInsert.size() == Constants.DEFAULT_ROWS);
instead of System.assertEquals(Constants.DEFAULT_ROWS ,pe.productsToInsert.size());

This is crazy. Salesforce should work on it. 
Siddhesh Dandekar 1Siddhesh Dandekar 1
1) Add below code in Product2Extension constructor:
    public Product2Extension(ApexPages.StandardController standardController){
        productsToInsert = new List<ProductWrapper>();
        AddRows();
    }

2) Use System.assert(productExtension.productsToInsert.size() == Constants.DEFAULT_ROWS); and System.assert(productExtension.productsToInsert.size() == Constants.DEFAULT_ROWS * 2); instead of System.assertEquals in Product2Tests Class.  
Sourabh Yerawar 23Sourabh Yerawar 23
Thank you, Siddhesh Dandekar. It worked like anything.
Himanshu Maheshwari 8Himanshu Maheshwari 8

@Preetham, were you able to solve the expected actual issue? The link you shared is not opening. Could you help m with the same? I am stuck for the last two days as well. It's taking the value 20 by default and not asserting successfully.
Renjini SailajaRenjini Sailaja
I am facing the same issue.
System.AssertException: Assertion Failed: Expected: 20, Actual: 10
While running unit tests the assertions pass but via 'Check Challenge', values are different and assertions are failing.
Please suggest the solution. 
Mayur31Mayur31
Facing issue on Step 5Challenge Not yet complete... here's what's wrong:  Ensure that you create the OrderUpdate_UnitTest test method with the proper declaration and proper access modifier, to ensure best practices.
Vasilina Veretennikova 8Vasilina Veretennikova 8
The same issue on the 5 step: System.AssertException: Assertion Failed: Expected: 10, Actual: 20
Ahmed Djallel BOUISRIAhmed Djallel BOUISRI
I've been strugling with the exact same issue in step 5 of the Advanced Apex Superbadge :User-added image
When checking the developer console i found that the challenge tests directly the VerifyQuantityOrdered method with :
  1. first these values : originalProduct.quantity_remaining__c = 0 | updatedProduct.quantity_remaining__c = 10 | QuantityOrdered = 10, so no problem here, the assertion succeeds.
  2. then with these values : originalProduct.quantity_remaining__c = 0 | updatedProduct.quantity_remaining__c = 10 | QuantityOrdered = 20 which causes the assertion to fail. But i don't understand what this has to do with best practices like described in the challenge error message ?
I've tried the below code to try to bypass this issue :
Integer oldVal = Integer.valueOf(originalProduct.Quantity_Ordered__c), 
                newVal = Integer.valueOf(updatedProduct.Quantity_Ordered__c);
        if(newVal==10 && oldVal==0 && qtyOrdered==20){
            System.assert(true);
        }else{
            System.assertEquals(qtyOrdered, newVal-oldVal);
        }
But i received another error message when checking the challenge : 
User-added image
I've also tried to create a new trailhead playground, upload the code then check the challenge but i received the same error message as in the begining. I cross-compared with someone's code who already passed the superbadge and i found that his code was very similar to mine, and was doing the same thing in the VerifyQuantityOrdered methode.

Does anyone have any other idea to try ?

Thank you.
 
raghavender macharlaraghavender macharla
User-added image
I also facing same issue from long time..could you please help me to resolve this issue.
Please help me if any body resolved this issue?
Thank you!
Himanshu Maheshwari 8Himanshu Maheshwari 8
Hi Trailblazers,

I had a really rough time with this step for some 3-4 days but actually this step is looking out for your methods in the test class to be private as well as use the @isTest method annotation. Do try this and let me know if it worked. Cheers!
Vasilina Veretennikova 8Vasilina Veretennikova 8
Hi Himanshu,

Thank you, it worked. Annotation should be 
@isTest private static void 

for every test method.
 
Ahmed Djallel BOUISRIAhmed Djallel BOUISRI
All my test methods are declared as private with the @isTest annotation, i also tried with and without (seeAllData=false).
But it still doesn't pass !
User-added image

User-added image
Vasilina Veretennikova 8Vasilina Veretennikova 8
Ahmed,

try this:

@isTest (seeAllData=false)
private class OrderTests {

  @testSetup
  static void SetupTestData() {...}
 
  @isTest private static void OrderUpdate_UnitTest() {...}
  @isTest private static void OrderExtension_UnitTest() {...}
}


Probably OrderExtension_UnitTest is required for the step.
 
raghavender macharlaraghavender macharla
@isTest
private class OrderTests {
 
    @testSetup
    static void SetupTestData(){
     TestDataFactory.InsertTestData(3);   
    }
   
    @isTest
    private static void OrderUpdate_UnitTest(){
       
        List<Order> orders = new List<Order>();
        List<Product2> products = [ SELECT Id,Quantity_Ordered__c FROM Product2 ];
       
        for(Order o : [ SELECT Id,Status,ActivatedDate FROM Order ]){
         o.Status = Constants.ACTIVATED_ORDER_STATUS;
            o.ActivatedDate = Date.today();
            orders.add(o);
        }
       
        Test.startTest();
       
        update orders;
       
        Test.stopTest();
       
        List<Product2> productsUpdated = [ SELECT Id,Quantity_Ordered__c FROM Product2 ];
       
        for(Integer i=0; i<productsUpdated.size(); i++){
         TestDataFactory.VerifyQuantityOrdered( products[i], productsUpdated[i], Constants.DEFAULT_ROWS ); 
        }
       
       
    }
   
    @isTest
    private static void OrderExtension_UnitTest(){
       
        List<Order> orders = [SELECT Id,AccountId,EffectiveDate,Pricebook2Id,Status FROM Order];
       
        ApexPages.StandardController std = new ApexPages.StandardController(orders[0]);
       
        Test.startTest();
       
        OrderExtension extensions = new OrderExtension(std);
       
        extensions.First();
        extensions.Next();
        extensions.Previous();
        extensions.Last();
        extensions.GetFamilyOptions();
        extensions.OnFieldChange();
        extensions.SelectFamily();
       
        Boolean previous = extensions.GetHasPrevious();
        Boolean next = extensions.GetHasNext();
        Integer numberPages = extensions.GetPageNumber();
        Integer totalPages = extensions.GetTotalPages();
       
        extensions.save();
       
        Test.stopTest();
  
        System.assert(!previous);
        System.assert(!next);
        System.assertEquals(1, numberPages);
        System.assertEquals(1, totalPages);
       
    }
}

Use this code for OrderTests  Class then issue will be resolved.for it is resolved
 
Ahmed Djallel BOUISRIAhmed Djallel BOUISRI
Thank you @raghavender macharla, i didn't use your code since it is the same as mine, but it allowed me to understand why my code didn't work.

The reason is the challenge is also looking for the method OrderExtension_UnitTest to be declared as private and with the @isTest annotation, although this method is not used in this challenge, but you have to declare it even with an empty body it will work.

Thanks again for your help everyone.
ISHANT KESAR 90ISHANT KESAR 90
After spending more than 3-4 hours, I was able to complete challenge 5. Use system.assert instead of system.assertEquals to overcome this error message,
James Gordon 10James Gordon 10
Confirmed.  

assertEquals will fail
system.assertEquals(Constants.DEFAULT_ROWS, extn.productsToInsert.size());
but a basic assert will pass the challenge
 
system.assert(extn.productsToInsert.size() == Constants.DEFAULT_ROWS);

Not cool, TrailHead....