You need to sign in to do that
Don't have an account?

Messaging.SingleEmail Test Coverage
Hello,
I have the below async class and its test.
What I failing with is how to cover test percentage for Messaging.SingleEmailMessage Invocation when callout fails..
I have already gone through multiple links on stack exchange and Apex forum, tried some approaches but none worked for me.
Can some one please assist in what I may be doing wrong here.
Code coverage under SUCCESS is 78% while under FAILURE is 24%.
Test Class...
Thanks!
I have the below async class and its test.
What I failing with is how to cover test percentage for Messaging.SingleEmailMessage Invocation when callout fails..
I have already gone through multiple links on stack exchange and Apex forum, tried some approaches but none worked for me.
Can some one please assist in what I may be doing wrong here.
Code coverage under SUCCESS is 78% while under FAILURE is 24%.
public class DeleteExpertInSelene { //method to be invoked by ProcessBuilder apex @InvocableMethod public static void deleteAccountInSelene(List<Id> acctIds){ Account acc = [SELECT Selene_ID__c FROM Account WHERE Id = :acctIds[0]]; System.enqueueJob(new QueueableSeleneCall(acc.Selene_ID__c, acc.Id)); } // future method to make delete experts @Future(callout=true) private static void deleteToSelene(Decimal userId, Id acctId) { List<String> EMAIL_RESULTS = new List<String>{System.Label.Email_Selene_List}; List<String> EMAIL_RESULTS_2 = new List<String>{System.Label.Email_Selene_List_2}; String tokenVar = ''; tokenVar = 'Token '+GetSeleneID.getAuthorizationToken(); HTTPRequest req = new HTTPRequest(); req.setEndPoint('some api'+ userId); req.setMethod('DELETE'); req.setHeader('authorization',tokenVar); req.setHeader('Accept','application/json;charset=UTF-8'); req.setHeader('Content-Type', 'application/json;charset=UTF-8'); req.setHeader('Cache-Control', 'no-cache'); req.setHeader('x-api-key', 'some key'); req.setTimeout(120000); HTTP http = new HTTP(); HTTPResponse res = http.send(req); if(res.getStatusCode() != 200){ System.debug('Failure: ' + res.getStatusCode() + ' ' + res.getStatus()); // Send email Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(EMAIL_RESULTS); mail.setCcAddresses(EMAIL_RESULTS_2); mail.setSenderDisplayName('Salesforce Administrator'); mail.setSubject('Delete To Selene Failed '); mail.setPlainTextBody('some mssg'); Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{mail}); } else { if(res.getStatusCode() == 200){ System.debug('Success: ' + res.getBody() + ' ' + res.getStatus()); } } } //queueable class to enque the put request class QueueableSeleneCall implements System.Queueable, Database.AllowsCallouts { private Decimal seleneId; private String Id; public QueueableSeleneCall(Decimal userId, Id acctId){ this.seleneId = userId; this.Id = acctId; } public void execute(QueueableContext context) { deleteToSelene(seleneId, Id); } } }
Test Class...
@isTest public class DeleteExpertInSelene_Test { @testSetup static void testSetupdata(){ //create the account record Account acc1 = new Account(); acc1.Name = 'ABC Corp1'; acc1.Email__c = 'test@testmail.com'; acc1.Phone = '5554446767'; acc1.Legacy_GID__c = '54321'; acc1.Display_Name__c = 'abc1'; acc1.Status__c = 'Live'; acc1.Selene_ID__c = 112233445.00; insert acc1; //create the account record Account acc2 = new Account(); acc2.Name = 'ABC Corp2'; acc2.Email__c = 'testmail@test.com'; acc2.Phone = '6665554343'; acc2.Legacy_GID__c = '12345'; acc2.Display_Name__c = 'abc2'; acc2.Status__c = 'Live'; acc2.Selene_ID__c = 112233446.00; insert acc2; } @isTest static void testPostCalloutSuccess() { Account acc = [Select Id, Name FROM Account WHERE Name = 'ABC Corp1' Limit 1]; List<Id> accList = new List<Id>(); accList.add(acc.Id); System.assertEquals('ABC Corp1', acc.Name); System.assertEquals(1,accList.size()); // Set mock callout class Test.setMock(HttpCalloutMock.class, new SeleneCalloutMockService()); // This causes a fake response to be sent // from the class that implements HttpCalloutMock. Test.startTest(); DeleteExpertInSelene.deleteAccountInSelene(accList); //Integer invocations = Limits.getEmailInvocations(); Test.stopTest(); // Verify that the response received contains fake values acc = [select Selene_ID__c from Account where id =: acc.Id]; System.assertEquals(112233445.00,acc.Selene_ID__c); } @isTest static void testPostCalloutFailure() { Account acc = [Select Id, Name FROM Account WHERE Name = 'ABC Corp2' Limit 1]; List<Id> accList = new List<Id>(); accList.add(acc.Id); System.assertEquals('ABC Corp2', acc.Name); System.assertEquals(1,accList.size()); // Set mock callout class Test.setMock(HttpCalloutMock.class, new SeleneCalloutMockService()); // This causes a fake response to be sent // from the class that implements HttpCalloutMock. Test.startTest(); DeleteExpertInSelene.deleteAccountInSelene(accList); Integer invocations = Limits.getEmailInvocations(); Test.stopTest(); // Verify that the response received contains fake values acc = [select Selene_ID__c from Account where id =: acc.Id]; System.assertEquals(112233446.00,acc.Selene_ID__c); System.assertEquals(1, invocations, 'An email should be sent'); } }Any help is appreciated.
Thanks!
if(res.getStatusCode() != 200 || Test.isRunningTest()){
{
All Answers
if(res.getStatusCode() != 200 || Test.isRunningTest()){
{
This worked brilliantly.
How ever, this is just bypassing the code.
Not exactly test coverage,
Still thanks.