0

I can't figure out how to get the testclass to complete successfully, although both Trigger and Class are at 90% coverage.

IT Keeps failing with the following error:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadMgmt_InsideSales: execution of AfterInsert

caused by: System.AsyncException: Future method cannot be called from a future or batch method: HTTPRequestSender_Class.sendHTTPRequest(String, String)

Trigger.LeadMgmt_InsideSales: line 20, column 1: []

Is the HTTPRequestSender_Test class even required here?

Trigger:

trigger LeadMgmt_InsideSales on Lead (after insert) {

ID[] ids = new List<ID>();
String url = 'https://inc1.insidesales.com/do=noauth/immediate_response';
String body;

for (Lead l : Trigger.new) {

    Group q = [Select id, name from Group where name = 'Inbound SDR Queue'];

    if (l.OwnerID == q.id) {
        ids.add(l.Id);
    }
}  

if (!ids.isEmpty()) {
   body = 'method=responsepop&inbound_initiative_id=60&ids='+EncodingUtil.urlEncode(JSON.serialize(ids), 'ISO-8859-1');       
   HTTPRequestSender_Class.sendHTTPRequest(url, body);
  }
}

Class:

global class HTTPRequestSender_Class {
@future (callout=true)
Public static void sendHTTPRequest(String url, String body){

    Http http = new Http();
    HttpResponse res = new HttpResponse();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setBody(body);
    req.setMethod('POST');

    try {
        res = http.send(req);
    } catch(System.CalloutException e) {
        System.debug('CalloutException: '+ e);
        System.debug(res.toString());
    }
 }
}

TestClass:

 @isTest
 public class HTTPRequestSender_Test {
 @isTest static void testCallout() {
    // Set mock callout class 
    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());      
    HttpResponse res = CalloutClass.getInfoFromExternalService();

  }
}

Callout Class

public class CalloutClass {
public static HttpResponse getInfoFromExternalService() {
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://inc1.insidesales.com/do=noauth/immediate_response');
    req.setMethod('GET');
    Http h = new Http();
    HttpResponse res = h.send(req);
    return res;
  }
}

MockGeneratorClass:

@isTest(SeeAllData=true)
global class MockHttpResponseGenerator implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest req) {

    Group q = [Select id, name from Group where name = 'Inbound SDR Queue'];

    Lead l = new Lead(LastName = 'Test', Company = 'ABC', Email = 'test@test.com', ownerid = q.Id);
    insert l;

    ID[] ids = new List<ID>();
    ids.add(l.Id);

 System.assertEquals('https://inc1.insidesales.com/do=noauth/immediate_response', req.getEndpoint());
    System.assertEquals('GET', req.getMethod());

    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'application/json');
    res.setBody('method=responsepop&inbound_initiative_id=60&ids='+EncodingUtil.urlEncode(JSON.serialize(ids), 'ISO-8859-1'));
    res.setStatusCode(200);
    return res;
}
}
0

TestClass:

@isTest
public class HTTPRequestSender_Test {
//@isTest static void testCallout() {
public static testmethod void testCallout() {

    Group q = [Select id, name from Group where name = 'Queue'];
    Database.DMLOptions dml = new Database.DMLOptions();
    dml.DuplicateRuleHeader.AllowSave = true;        
    Lead l = new Lead(LastName = 'Test', Company = 'ABC', Email = 'test@test.com', ownerid = q.Id);
    l.setOptions(dml);

    Test.startTest();
    Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorIS_Test());        
    insert l;
    Test.stopTest();

    HttpResponse res = CalloutClass.getInfoFromExternalService();

    // Verify response received contains fake values
    String contentType = res.getHeader('Content-Type');
    System.assert(contentType == 'application/json');
    String actualValue = res.getBody();
    String expectedValue = 'method=responsepop&inbound_initiative_id=213&ids='+EncodingUtil.urlEncode(JSON.serialize(l.Id), 'ISO-8859-1');
    System.assertEquals(actualValue, expectedValue);
    System.assertEquals(200, res.getStatusCode());

}
}

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.