1

While I am working on my Apex Specialist super badge, I am got this error message while I am testing my WebService class. Here is the Callout class:

public with sharing class WarehouseCalloutService {

private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';

@future(callout=true)
public static void runWarehouseEquipmentSync(){
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(WAREHOUSE_URL);
    request.setMethod('GET');
    HttpResponse response = http.send(request);
    if (response.getStatusCode() == 200) {
        List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
        List<Product2> equipmentList = new List<Product2>();
        for(Object obj: results){
            Map<String, Object> mapJson = (Map<String, Object>)obj;
            Product2 product = new Product2();

            Integer maintenanceperiod = (Integer)mapJson.get('maintenanceperiod');               
            Integer Lifespan = (Integer)mapJson.get('lifespan');
            Integer Cost = (Integer)mapJson.get('cost');
            Boolean replacement = (Boolean)mapJson.get('replacement');
            Integer quantity = ((Integer)mapJson.get('quantity'));

            product.Replacement_Part__c = replacement;
            product.Current_Inventory__c = quantity;
            product.Name = (String)mapJson.get('name');
            product.Maintenance_Cycle__c = maintenanceperiod;
            product.Lifespan_Months__c = Lifespan;
            product.Cost__c = Cost;
            product.Warehouse_SKU__c = (String)mapJson.get('sku');
            Product.ProductCode = (String)mapJson.get('_id');
            equipmentList.add(product);
        }
        if(!equipmentList.isEmpty()){
            upsert equipmentList;
        }
    }
  }
} 

Here is my test class:

@isTest
private class WarehouseCalloutServiceTest {

   @isTest static void testGetCallout() {

    Boolean expectedReplacement = false;
    String expectedName = 'Generator 3000 kW';
    String expectedSku = '100071';
    Integer expectedMaintenanceperiod = 345;
    Integer expectedLifespan = 110;
    Integer expectedCost = 5000;
    Integer expectedQuantity = 5;

    Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());
    Test.startTest();
    WarehouseCalloutService.runWarehouseEquipmentSync();
    Test.stopTest();

    Product2 equipment = [SELECT Id, Current_Inventory__c, Replacement_Part__c, Name, Maintenance_Cycle__c, Lifespan_Months__c, Cost__c, Warehouse_SKU__c
                          FROM Product2];
    System.assertEquals(equipment.Current_Inventory__c, expectedQuantity);                
    System.assertEquals(equipment.Replacement_Part__c, expectedReplacement);
    System.assertEquals(equipment.Name, expectedName);
    System.assertEquals(equipment.Maintenance_Cycle__c, expectedMaintenanceperiod);
    System.assertEquals(equipment.Lifespan_Months__c, expectedLifespan);
    System.assertEquals(equipment.Cost__c, expectedCost);
    System.assertEquals(equipment.Warehouse_SKU__c, expectedSku);

 }
}

Mock class:

@isTest
global class WarehouseCalloutServiceMock implements HttpCalloutMock {
// implement http mock callout

    global HTTPResponse respond(HTTPRequest request) {
    // Create a fake response
    HttpResponse response = new HttpResponse();
    response.setHeader('Content-Type', 'application/json');
    response.setBody('{"_id":"55d66226726b611100aaf744","replacement":false,"quantity":5,"name":"Generator 3000 kW","maintenanceperiod":345,"lifespan":110,"cost":5000,"sku":"100071"}');
    response.setStatusCode(200);
    return response; 
  }
}

Error screenshot:

enter image description here

This looks like pretty straight forward and I am not sure why I am seeing this error. I do not want to take someone's work and complete my badge, so trying to understand my mistake here and a possible workaround.

  • Do you have a time number where this error is thrown? – Zack Walton Feb 7 '19 at 1:13
  • @ZackWalton Added the screenshot. – Austin Evans Feb 7 '19 at 1:17
  • The problem is in your mock, I think. Can we see that? – sfdcfox Feb 7 '19 at 1:20
  • @sfdcfox Updated with the Mock class too. – Austin Evans Feb 7 '19 at 1:30
3

Your code is just fine, your mock class seems to be the culprit. Make sure you're returning JSON like the original, including the outer [...], which makes it a list. If the first character in your JSON is {, it will be a Map<String, ANY>.

Note: Your System.assertEquals method parameters are backwards; the first value is the expected value, the second is the actual value. You'll get nonsensical errors if the test fails from the assertions otherwise.

| improve this answer | |
  • Well, I found the mistake. yes, the mock class is the culprit. – Austin Evans Feb 7 '19 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.