You need to sign in to do that
Don't have an account?
divya1234
Challenge Not yet complete... here's what's wrong: Could not find a class named 'WarehouseCalloutServiceMock' containing a mock callout response.
I have developed some code for Step 5 in the Superbadge, but appear to stuck as I run the test. I am encountering an error and attempting to determine how to configure my code to pass the test. Are there any steps I am missing? I've covered much of the superbadge, but remain stuck here.
Please Help!
@isTest global class WarehouseCalloutServiceMock implements HttpCalloutMock { // implement http mock callout global HttpResponse respond(HttpRequest request){ System.assertEquals('https://th-superbadge-apex.herokuapp.com/equipment', request.getEndpoint()); System.assertEquals('GET', request.getMethod()); // Create a fake response HttpResponse response = new HttpResponse(); response.setHeader('Content-Type', 'application/json'); response.setBody('[{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"}]'); response.setStatusCode(200); return response; } }
@isTest private class WarehouseCalloutServiceTest { // implement your mock callout test here @isTest static void WarehouseEquipmentSync(){ Test.startTest(); // Set mock callout class Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock()); // This causes a fake response to be sent from the class that implements HttpCalloutMock. WarehouseCalloutService.runWarehouseEquipmentSync(); Test.stopTest(); System.assertEquals(1, [SELECT count() FROM Product2]); } }
public with sharing class WarehouseCalloutService { private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment'; // complete this method to make the callout (using @future) to the // REST endpoint and update equipment on hand. @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 the request is successful, parse the JSON response. if (response.getStatusCode() == 200) { // Deserialize the JSON string into collections of primitive data types. List<Object> equipments = (List<Object>) JSON.deserializeUntyped(response.getBody()); List<Product2> products = new List<Product2>(); for(Object o : equipments){ Map<String, Object> mapProduct = (Map<String, Object>)o; Product2 product = new Product2(); product.Name = (String)mapProduct.get('name'); product.Cost__c = (Integer)mapProduct.get('cost'); product.Current_Inventory__c = (Integer)mapProduct.get('quantity'); product.Maintenance_Cycle__c = (Integer)mapProduct.get('maintenanceperiod'); product.Replacement_Part__c = (Boolean)mapProduct.get('replacement'); product.Lifespan_Months__c = (Integer)mapProduct.get('lifespan'); product.Warehouse_SKU__c = (String)mapProduct.get('sku'); product.ProductCode = (String)mapProduct.get('_id'); products.add(product); } if(products.size() > 0){ System.debug(products); upsert products; } } } }
Please Help!
Please define your method as static in class WarehouseCalloutServiceMock and try again.
global class WarehouseCalloutServiceMock implements HttpCalloutMock {
// implement http mock callout
global static HttpResponse respond(HttpRequest request){
System.assertEquals('https://th-superbadge-apex.herokuapp.com/equipment', request.getEndpoint());
System.assertEquals('GET', request.getMethod());
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('[{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"}]');
response.setStatusCode(200);
return response;
}
}
Let me know if it helps.
Thanks
Thanks
Rahul Jain
Please check below URL, you will pass this challenge :
https://github.com/abhishekcse122/Apex-Specialist-Superbadge
apex class == WarehouseSyncSchedule
global class WarehouseSyncSchedule implements Queueable {
global void execute(System.QueueableContext SC) {
System.enqueueJob(new WarehouseCalloutService());
}
private static String CRON_EXP = '0 0 0 * * ? 2020';
public static Id scheduleMorningRun() {
Integer pstTargetHour = 1; // 24 hour time
Integer pstOffset = -8;
Integer userOffset = UserInfo.getTimeZone().getOffset(Date.today());
Integer userTime = Math.mod(pstTargetHour - pstOffset + userOffSet, 24);
System.debug('==15='+pstTargetHour);
System.debug('==16='+pstOffset);
System.debug('===17='+userOffset);
System.debug('===18='+userTime);
String schedule = '0 1 0 1 1/1 ? *';
System.debug('===19='+schedule);
return System.enqueueJob(new WarehouseSyncSchedule());
}
}
===============================================================================================================
@isTest(seeallData = true)
private class WarehouseSyncScheduleTest {
//private static final User TestRunner = TEST_RunAsUserFactory.create();
private static String CRON_EXP = '0 0 0 * * ? 2022';
@isTest static void testScheduleMorningRun(){
Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());
Id resultJobId;
Test.startTest();{
WarehouseSyncSchedule URL = new WarehouseSyncSchedule();
}
resultJobId = WarehouseSyncSchedule.scheduleMorningRun();
System.enqueueJob(new WarehouseSyncSchedule());
Test.stopTest();
System.assertEquals('Test', resultJobId) ;
}
}
Try this One you will get 100% test coverage.
Thank you...