I have a problem making a testclass.
I need to simulate an exception for this class:
public class ScheduleOmniAlta50 implements Queueable, Database.AllowsCallouts {
private String body;
private String division;
private static Integer maxRetries = 2;
private static Integer retryIntervalMinutes = 1;
public ScheduleOmniAlta50(String body, String division) {
this.body = body;
this.division = division;
}
public void execute(QueueableContext context) {
try {
WSSem.Alta50Response resp = WSSem.sendAlta50(body, division);
} catch (Exception e) {
if (AsyncInfo.getCurrentQueueableStackDepth() < maxRetries) {
ScheduleOmniAlta50 sc = new ScheduleOmniAlta50(body, division);
ID jobID = System.enqueueJob(sc, retryIntervalMinutes);
}
}
}
}
My test is not going to the catch
@isTest
public class ScheduleOmniAlta50Test implements Database.AllowsCallouts{
static XHttpCalloutMock getAlta50ResponseError;
static {
getAlta50ResponseError = new XHttpCalloutMock();
getAlta50ResponseError.buildResponse()
.withStatus('OK')
.withStatusCode(200)
.withBody(SemAuthMock.getTokenResponse())
.addForAddress(SemAuth.SERVICE_CONFIG.URL__c + '/api/token');
getAlta50ResponseError.buildResponse()
.withStatus('ERROR')
.withStatusCode(404)
.withBody('{"error": "Error en la solicitud", "hasError" : true}')
.addForAddress(
WSSem.SERVICE_CONFIG.URL__c + '/api/Validations/FinalStage'
);
}
@isTest
static void testExecute_CalloutExceptionThrown() {
Test.setMock(HttpCalloutMock.class, getAlta50ResponseError);
ScheduleOmniAlta50 scheduleJob = new ScheduleOmniAlta50('Datos de prueba', 'División de prueba');
Test.startTest();
scheduleJob.execute(null);
Test.stopTest();
List<AsyncApexJob> retryJobs = [SELECT Id, Status FROM AsyncApexJob];
System.assertNotEquals(0, retryJobs.size(), 'Debería haber al menos un reintento programado');
}
}
Thanks.
WSSem.Alta50Response resp = WSSem.sendAlta50(body, division);
iin a class and use dependency injection to tell the class at testmethod time to throw an exception; advanced approach for dependency injection is apexmocks doThrowWhen method