Apex Class

public with sharing class WarehouseCalloutService {

private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
public static List<EquipmentWrapperFromJSON> wrapWquipment;
// complete this method to make the callout (using @future) to the
// REST endpoint and update equipment on hand.
public static void runWarehouseEquipmentSync(){
    List<Product2> UpdateProductsFROMinventory = new List<product2>();
    //List<Product2> newProdListFromWH = new List<product2>();
    Map<string, Product2> Products = new Map<string, Product2>();
    for(Product2 p2 : [SELECT ID, Name, Warehouse_SKU__c FROM Product2 WHERE Warehouse_SKU__c != Null]){
        Products.put(p2.Warehouse_SKU__c, p2);
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    HttpResponse response = new HttpResponse();

    response = http.send(request);

    // If the request is successful, parse the JSON response.
    if (response.getStatusCode() == 200) {
        system.debug('---Connection made successfully--');

        //List<Object> equipments = (List<Object>) JSON.deserializeUntyped(response.getBody());
        wrapWquipment = (List<EquipmentWrapperFromJSON>) JSON.deserialize(response.getBody(), List<EquipmentWrapperFromJSON>.class);
        //System.debug('Received the following equipment:'+wrapWquipment);
        for(EquipmentWrapperFromJSON eqp : wrapWquipment){
                system.debug('---found equipment--'+eqp.Name);
                Product2 p = Products.get(eqp.sku);

                p.Replacement_Part__c = true;
                p.Name = eqp.Name;
                p.Cost__c = eqp.cost;
                p.Current_Inventory__c = eqp.quantity;
                p.Lifespan_Months__c = eqp.lifespan;
                p.Maintenance_Cycle__c = eqp.maintenanceperiod;

                Product2 pp = new Product2();

                pp.Warehouse_SKU__c = eqp.sku;
                pp.Name = eqp.Name;
                pp.Replacement_Part__c = true;
                pp.Cost__c = eqp.cost;
                pp.Current_Inventory__c = eqp.quantity;
                pp.Lifespan_Months__c = eqp.lifespan;
                pp.Maintenance_Cycle__c = eqp.maintenanceperiod;

            upsert UpdateProductsFROMinventory;


JSON Wrapper

public class EquipmentWrapperFromJSON {

    public Boolean replacement ;
    public Integer quantity ;
    public integer maintenanceperiod ;
    public Integer lifespan ;
  public integer cost ;
   public string sku ;
  public string Name ;}

Test class

private class WarehouseCalloutServiceTest {
  // implement your mock callout test here
    testMethod Static void test(){
        List<EquipmentWrapperFromJSON> ListEWFS = new List<EquipmentWrapperFromJSON>();
        List<Product2> listProduct = new List<Product2>();
        Product2 p = new Product2();
        p.Replacement_Part__c = true;
        p.Name = 'testing';
        p.Cost__c = 10;
        p.Current_Inventory__c = 5;
        p.Lifespan_Months__c = 2;
        p.Maintenance_Cycle__c = 60;
        p.Warehouse_SKU__c = '10001';

        insert listProduct;

            //ListEWFS = WarehouseCalloutService.wrapWquipment();
            Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());
        Upsert listProduct;

Mock Response

global class WarehouseCalloutServiceMock implements HttpCalloutMock {
    // implement http mock callout
    global Httpresponse respond(HTTPRequest request){
        HTTPResponse response = new HTTPResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"equipment":[{"id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"}, {"id":"55d66226726b611100aaf742","replacement":true,"quantity":183,"name":"Cooling Fan","maintenanceperiod":0,"lifespan":0,"cost":300,"sku":"100004"}]}');
        return response;
  • 2
    The Trailhead superbadges aren't really intended as something that others would take you through step by step to complete. See Trailhead - Where Do We Draw the Line?. The idea is for you to demonstrate mastery of all the various aspects of Salesforce. Consider removing the majority of your code from the question and instead focusing on a very specific problem you are having. Sep 19, 2016 at 8:07
  • It's really hard to answer the question like this, you could help us by limiting your code to the parts you're not sure how to cover and explain what you've already attempted to cover those specifically. It helps to remove some use-case specific lines but focus on the large structure of the code. Sep 20, 2016 at 18:47
  • 5
    I'm voting to close this question as off-topic because Trailhead exercise
    – Eric
    Sep 20, 2016 at 20:36

1 Answer 1


As you are using a mock, you should remove the:


guard around the http.send as you want the send to be done when the test runs.

Then coverage mostly depends on the WarehouseCalloutServiceMock returning a HttpResponse sufficiently filled in for the code after the http.send to all execute.


The easiest way for you mock to return valid JSON is to create the correct type of objects in the mock and then use JSON.serialize to create the JSON string. Alternatively you can manually construct the string.


In JSON an array (list) starts and ends like this [...] so your mock response should too. See http://json.org/ for the syntax of JSON.

  • Yes removed, and getting : System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
    – RaJu
    Sep 16, 2016 at 8:22
  • @RaJu See the PS; you will have to fix/add/improve the code in your WarehouseCalloutServiceMock.
    – Keith C
    Sep 16, 2016 at 8:27
  • Please suggest me how to proceed if possible,thanks @keith C
    – RaJu
    Sep 16, 2016 at 8:39
  • @RaJu Best you add your mock class to the question then someone may be willing to propose code changes to it.
    – Keith C
    Sep 16, 2016 at 9:04
  • Still not able to figured it out, help needed to complete the trailhead superbadge. Further help on writing test class for future method which makes callouts would be be helpful for me. @Keith C
    – RaJu
    Sep 19, 2016 at 6:11

Not the answer you're looking for? Browse other questions tagged .