You need to sign in to do that
Don't have an account?
Ryan Greene
Test for a Class which pulls info from Trigger
Hello all,
I am having trouble with an HTTP Callout Test. I have a Trigger which sends it's info to the Class which houses the Http Callout. To Test for an Http Callout I followed the Apex Dev documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm) where I create two Test classes, one sends Mock Http info and the other calls the Mock info. I beleive the problem is in the passing the "String ldid" from the Trigger to the Class. Trigger/Class/Tests below. Any ideas on how to solve this? Thanks!
Trigger:
I am having trouble with an HTTP Callout Test. I have a Trigger which sends it's info to the Class which houses the Http Callout. To Test for an Http Callout I followed the Apex Dev documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm) where I create two Test classes, one sends Mock Http info and the other calls the Mock info. I beleive the problem is in the passing the "String ldid" from the Trigger to the Class. Trigger/Class/Tests below. Any ideas on how to solve this? Thanks!
Trigger:
String ldid; if(Trigger.isBefore){ if(Trigger.isUpdate || Trigger.isInsert){ for(Lead ld : Trigger.new){ if(ld.IsConverted == TRUE && ld.Segment__c == 'Large Group'){ ldid = ld.Id; CallApply.basicAuthCallout(ldid);Class:
public class CallApply { public static HttpResponse basicAuthCallout(String ldid){ HttpRequest req = new HttpRequest(); req.setEndpoint('https://test'); req.setMethod('POST'); String body = '{"LeadId":"'+ldid+'"}'; req.setHeader('Content-Type','application/json'); req.setBody(body); Http http = new Http(); HTTPResponse res = http.send(req); return res; } }Test1:
@isTest global class Test_callApply1 implements HttpCalloutMock { global HTTPResponse respond(HttpRequest req) { HttpResponse res = new HttpResponse(); res.setHeader('Content-Type','application/json'); res.setBody('{"example":"test"}'); res.setStatusCode(200); return res; } }Test2:
@isTest private class Test_callApply2 { @isTest static void testCallout() { Test.setMock(HttpCalloutMock.class, new Test_callApply1()); //create Lead to pass value 'ldid' Lead ld = new Lead(); ld.FirstName = 'testRyan'; ld.LastName = 'testGreene'; ld.email = 'testingtrigger1@test.com'; ld.Company = 'testCompanyasdasdasdeyjhlnn'; insert ld; String ldid = ld.id; HttpResponse res = CallApply.basicAuthCallout(ldid); String contentType = res.getHeader('Content-Type'); System.assert(contentType == 'application/json'); String actualValue = res.getBody(); String expectedValue = '{"example":"test"}'; System.assertEquals(actualValue, expectedValue); System.assertEquals(200, res.getStatusCode()); } }