Skip to main content

I consume a web servicio type REST.

1.- I create my class apex without fails.

2.- I create my class Test without fails, when execute the test it happen a fail with the web service.

3.- I create a class Mock but i thing is not correctly.

 

The Question is how to do relationship correctly between Class TEST and Class Mock ?

 

1.- This is Class Apex:

public static Map<String, Object> getEstatusSupPermanente(String accountId){

String ctaFact;

Map<String, Object> datos = new Map<String, Object>();

datos.put('msj', 'OK');

List<CuentaFactura__c> idBRM = [select Id, IdCuentaBRM__c from CuentaFactura__c where id = :accountId];

for(CuentaFactura__c cf : idBRM){

ctaFact = cf.IdCuentaBRM__c;

}

Http http = new Http();

HttpRequest request = new HttpRequest();

request.setEndpoint(EndPoint__c.getValues('StatusSupresionPermanente').URL__c);

request.setMethod('POST');

request.setHeader('Content-Type', 'application/json;charset=UTF-8');

request.setHeader('Accept', 'application/json');

// Set the body as a JSON object

request.setBody(datosCuenta);

HttpResponse response;

if (Test.isRunningTest()){

Test.setMock(HttpCalloutMock.class, new movimientosCobranza_Mock());

response = http.send(request);

} else{

response = http.send(request);

}

// PARSE THE JSON RESPONSE.

if (response.getStatusCode() == 200) {

datos = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());

List<Object> listax = (List<Object>) datos.get('ResponseBRM');

datos.put('arreglo',listax[0]);

} else {

datos.put('msj', 'error');

datos.put('response', response);

}

return datos;

}

2.- This is Class Test:

String idCtaFact;

List<CuentaFactura__c> ctaFact = [SELECT Id, IdCuentaBRM__c FROM CuentaFactura__c LIMIT 1];

for(CuentaFactura__c cf : ctaFact){

idCtaFact = cf.Id;

}

Test.startTest();

movimientosCobranza_CTRL.getEstatusSupPermanente(idCtaFact);

Test.stopTest();

3.- This is Class Mock:

global HTTPResponse respond(HTTPRequest request){

String jsonTest = '{\n' +

'"ResponseBRM":{\n' +

'"AUTHORIZATION_ID":"SUSPO_A_X",\n' +

'"VALUE":"1",\n' +

'"CREATED_T":"2021-05-17T00:00:00"\n' +

'},\n' +

'}';

HttpResponse response = new HttpResponse();

response.setHeader('Content-Type', 'application/json');

// response.setBody('{"ResponseBRM":"12345"}');

response.setBody(jsonTest);

response.setStatusCode(200);

return response;

}

#Salesforce #Ask An Expert #Apex #Apex Test Classes #Collaboration #Trailhead Challenges #TrailblazerCommunityHelp

2 respuestas
  1. 4 sept 2021, 07:09

    hello Cristino,

     

    1.) Have you implemented  HttpCalloutMock class to your mock class ? (Please Check) 

     

    Example -

    @isTest global class <MockClassName> implements HttpCalloutMock

     

    2.) Have you set the Mock class to your test class ?

     

    Example -

     

    Test.setMock(HttpCalloutMock.class, new <MockClassName>());

     

    Please check it. hope it will help you, if it helps Mark It as Best answer.

0/9000