+ Start a Discussion
Martha VMartha V 

advanced apex superbadge error - Ensure that you create the OrderUpdate_UnitTest test method with the proper declaration and proper access modifier, to ensure best practices.

This is in Step 5 of the Superbadge. here is my test class
@isTest
private class OrderTests {
	@TestSetup   
    static void SetupTestData(){
        TestDataFactory.InsertTestData(5);
    }

    @isTest
    static void OrderUpdate_UnitTest(){
        //retrieve the orders saved by TestDataFactory
        Map<Id, Order> orders= new Map<Id, Order>([SELECT Id, Status FROM Order]);
        //save the original Product2 record in a map
        Map<Id, Product2> originalProductMap = new Map<Id, Product2>();
        //Loop through a query of OrderItems related to the orders
        List<OrderItem> items = [SELECT Id, Product2Id, Product2.Quantity_Ordered__c, Quantity
                                 FROM OrderItem
                                 WHERE OrderId IN :orders.keySet()];
        //ToDo: Populate the map with the Id of the related Product2 as the key and Product2 record as the value
        for ( OrderItem oi : items){
            Product2 p = oi.Product2;
            if (!originalProductMap.containsKey(p.Id)) {
                originalProductMap.put(p.Id, p);
            } 
        }
        //update the Status of the orders
        List<Order> updatedOrders = new List<Order>();
        for (Order order : orders.values()) {
            order.Status = Constants.ACTIVATED_ORDER_STATUS;
            updatedOrders.add(order);
        }
        //get the amounts to be order for each Product
        //declare a map to save the Amounts for each product
        Map<Id, Integer> prodAmounts = new Map<Id, Integer>();
        //get the aggregated amounts by product id
        AggregateResult[] groupedResults  = [SELECT Product2Id,SUM(Product2.Quantity_Ordered__c), SUM(Quantity) FROM OrderItem
                                             WHERE Product2Id IN :originalProductMap.keySet() 
                                             GROUP BY Product2Id];
        for (AggregateResult ar : groupedResults)  {
             prodAmounts.put(String.valueOf(ar.get('Product2Id')),Integer.valueOf(ar.get('expr1')));
        }                                     
        //update the orders in the DB
        update updatedOrders;
        //retrieve the products affected by the orders update
        Map<Id, Product2> updatedProductMap = new Map<Id, Product2>([SELECT id, Quantity_Ordered__c 
                                                                     FROM Product2 WHERE
                                                                     id IN :originalProductMap.keySet()]);
        for (Product2 origProd : originalProductMap.values()) {
            TestDataFactory.VerifyQuantityOrdered(origProd,updatedProductMap.get(origProd.Id), prodAmounts.get(origProd.Id));
        }
    }
}

Can anyone tell me why I am getting that error? my tests are running fine and I have enough code coverage on the extension, the order trigger and the order helper (which is what this step is about).

What am I missing? I have tried to chang @IsTest for TestMethod, but it still doesn't work.
Raj VakatiRaj Vakati
Use this code

https://developer.salesforce.com/forums/?id=9060G0000005OGJQA2
 
@isTest
public class OrderTests {

  @testSetup static void SetupTestData(){
    TestDataFactory.InsertTestData(1);
  }


  @isTest static void OrderUpdate_UnitTest(){

    Test.startTest();
    Order o = [SELECT Id, Status FROM Order LIMIT 1];
    Product2 p = [SELECT Id, Family, Name, Quantity_Ordered__c, Quantity_Remaining__c FROM Product2 LIMIT 1];

    o.status = constants.ACTIVATED_ORDER_STATUS;
    Update o;
    Product2 updatedp = [SELECT Id, Family, Name, Quantity_Ordered__c, Quantity_Remaining__c FROM Product2 LIMIT 1];


    TestDataFactory.VerifyQuantityOrdered(p, updatedp, constants.DEFAULT_ROWS);
    Test.stopTest();

  }

  @isTest static void OrderExtension_UnitTest() {
    PageReference pageRef = Page.OrderEdit;
    Test.setCurrentPage(pageRef);
    Order o = [SELECT Id, Status FROM Order LIMIT 1];
    ApexPages.StandardController stdcontroller = new ApexPages.StandardController(o);
    OrderExtension ext = new OrderExtension(stdcontroller);
    System.assertEquals(1, ext.orderItemList.size());
    ext.OnFieldChange();
    ext.SelectFamily();
    ext.Save();
    ext.First();
    ext.Next();
    ext.Previous();
    ext.Last();
    ext.GetHasPrevious();
    ext.GetHasNext();
    ext.GetTotalPages();
    ext.GetPageNumber();
    List<SelectOption> options = ext.GetFamilyOptions();
  }

  // static TestMethod void Product2Trigger_UnitTest(){
  //   Test.startTest();
  //   Product2 p = new Product2();
  //   p.Name = 'Test Product';
  //   p.Family= 'Side';
  //   p.IsActive= true;
  //   p.Quantity_Ordered__c = 50;
  //   p.Initial_Inventory__c = 100;
  //   insert p;
  //
  //   // CollaborationGroup cgroup = new CollaborationGroup();
  //   // cgroup.Name='TEST'+Constants.INVENTORY_ANNOUNCEMENTS;
  //   // cgroup.Description='test';
  //   // cgroup.CollaborationType='Public';
  //   // cgroup.IsArchived = false;
  //   // cgroup.IsAutoArchiveDisabled = false;
  //   // insert cgroup;
  //
  //   p.Quantity_Ordered__c=96;
  //   update p;
  //   Test.stopTest();
  // }

}


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

  /**
   * @name constructCollaborationGroup
   * @description
   **/
  public static CollaborationGroup ConstructCollaborationGroup(){
    CollaborationGroup cgroup = new CollaborationGroup();
    cgroup.Name = 'TEST' + Constants.INVENTORY_ANNOUNCEMENTS;
    cgroup.CanHaveGuests = false;
    cgroup.CollaborationType = 'Public';
    cgroup.IsArchived = false;
    cgroup.IsAutoArchiveDisabled = false;
    return cgroup;
  }

  /**
   * @name CreateProducts
   * @description constructs a List of Product2 records for unit tests
   **/
  public static List<Product2> constructProducts(Integer cnt){
    List<Schema.PickListEntry> familyValueList = Product2.Family.getDescribe().getPickListValues();
    Integer listSize = familyValueList.size();

    List<Product2> productList = new List<Product2>();
    for (Integer i = 0; i < cnt; i++) {
      Product2 p = new Product2();
      p.Name = 'Product ' + i;
      p.Family = familyValueList[Math.mod(i, listSize)].getValue();
      p.Initial_Inventory__c = 10;
      p.IsActive = true;
      productList.add(p);
    }
    return productList;
  }

  /**
   * @name CreatePricebookEntries
   * @description constructs a List of PricebookEntry records for unit tests
   **/
  public static List<PricebookEntry> constructPricebookEntries(List<Product2> productList){
    List<PricebookEntry> pbes = new List<PricebookEntry>();
    for (Product2 product: productList) {
      PricebookEntry pbe = new PricebookEntry();
      pbe.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
      pbe.Product2Id = product.Id;
      pbe.IsActive = true;
      pbe.UnitPrice = 1;
      pbes.add(pbe);
    }
    return pbes;
  }

  /**
   * @name CreateAccounts
   * @description constructs a List of Account records for unit tests
   **/
  public static List<Account> constructAccounts(Integer cnt){
    List<Account> accts = new List<Account>();
    for (Integer i = 0; i < cnt; i++) {
      Account acct = new Account();
      acct.Name = 'Account ' + i;
      accts.add(acct);
    }
    return accts;
  }

  /**
   * @name CreateContacts
   * @description constructs a List of Contacxt records for unit tests
   **/
  public static List<Contact> constructContacts(Integer cnt, List<Account> accts){
    Integer listSize = accts.size();

    List<Contact> contactList = new List<Contact>();
    for (Integer i = 0; i < cnt; i++) {
      Contact c = new Contact();
      c.LastName = 'Contact ' + i;
      c.AccountId = accts[Math.mod(i, listSize)].Id;
      contactList.add(c);
    }

    return contactList;
  }

  /**
   * @name CreateOrders
   * @description constructs a List of Order records for unit tests
   **/
  public static List<Order> constructOrders(Integer cnt, List<Account> accts){
    Integer listSize = accts.size();

    List<Order> orders = new List<Order>();

    for (Integer i = 0; i < cnt; i++) {
      Order o = new Order();
      o.Name = 'Order ' + i;
      o.AccountId = accts[Math.mod(i, listSize)].Id;
      o.EffectiveDate = Date.today();
      o.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
      o.Status = 'Draft';
      orders.add(o);
    }

    return orders;
  }

  /**
   * @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){
    Integer pbeListSize = pbes.size();
    Integer orderListSize = ords.size();

    List<OrderItem> orderItemList = new List<OrderItem>();

    for (Integer i = 0; i < cnt; i++) {
      OrderItem oi = new OrderItem();
      oi.OrderId = ords[Math.mod(i, orderListSize)].Id;
      oi.PriceBookEntryId = pbes[Math.mod(i, pbeListSize)].Id;
      oi.Quantity = Constants.DEFAULT_ROWS;
      oi.UnitPrice = 1;
      orderItemList.add(oi);
    }

    return orderItemList;
  }

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

  /**
   * @name SetupTestData
   * @description Inserts accounts, contacts, Products, PricebookEntries, Orders, and OrderItems.
   **/
  public static void InsertTestData(Integer cnt){
    insert constructCollaborationGroup();

    List<Product2> productList = constructProducts(cnt);
    insert productList;

    List<PricebookEntry> pbes = constructPricebookEntries(productList);
    insert pbes;

    List<Account> accts = constructAccounts(cnt);
    insert accts;
    insert constructContacts(cnt, accts);

    List<Order> ords = constructOrders(cnt, accts);
    insert ords;

    insert constructOrderItems(cnt, pbes, ords);
  }
}

 
Martha VMartha V
@Raj Vakati,

Thanks for your response. I tried your code, and it is still giving me the same error
error message from trailhead