0

Hi I have the below trigger and future pls help me on how to write Test class for this:-

    trigger Contactcallout on Contact (after update) {
    Map<Id, String> m = new Map<Id, String>();
    for (contact c : Trigger.new) {
    if(c.RecordTypeId == '012D0000000BaFA'){
    contact old = Trigger.oldMap.get(c.Id);   
    contact[] objectnew=[Select name, Account.Name from contact where id in: 
    Trigger.new];               
    string accName=objectnew[0].Account.Name;   

    if (c.Email            !=old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||accname!=null)             
  {
 WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);

        }
    }
    }
    }

My class

    public class WebServiceCallout {

    @future (callout=true)
    public static void sendNotification(string Id,string Email,string First_Name,string Last_Name,string phone,string Title,string Account_name,string con_status) {
conweb cont=new conweb(Id,Email,First_Name,Last_Name,Phone,Title,Account_name,con_status);
list<conweb> conwebs=new list<conweb>();
conwebs.add(cont);
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
req.setTimeout(2000); // timeout in milliseconds
        req.setEndpoint('http://test');
        req.setMethod('POST');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Authorization','test);
       req.setBody(JSON.serialize(conwebs[0]));
        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }

    }
 public class conweb
    {
    String salesForceId;
    String emailAddress;
    String firstName;
    string lastName;
    string contactNumber;
    string jobTitle;
    string companyName;
    string status;

    public conweb(string sid,string semail,string sfirstname,string  slastname,string sphone,string Title,string Account_name,string con_status)

    {
    salesForceId=sid;
    emailAddress=semail;
    firstName=sfirstname;
    lastName=slastname;
    contactNumber=sPhone;
    jobTitle=Title;
    companyName=Account_name;
    status=con_status;

    }
    }}

Pls help Many Thanks in advance

  • What do you need help on - specifically ? Writing a test class is quite a broad subject which is well documented, what is specifically not clear to you on how to proceed ? Try breaking down the process of writing a test class in smaller steps, we are best at helping if you can tell us which step is the problem here. – Samuel De Rycke Jun 16 '15 at 9:43
0

Abraham

I am providing a link that will surely help you , still if you have any issue then let me know.

Refer link:- https://developer.salesforce.com/forums/?id=906F000000094QOIAY

0

If you change the method sendNotification to return a copy of the method you could just call it directly and check the return, If you wish to test the full flow of your code instead of smaller checks on smaller methods;

Test.starttest() and Test.StopTest

will trigger the future call. But with no return from your sendNotification method without assertions or debugs you would not know if it works.

0

You need 2 things here.

  1. Use Test.startTest & Test.stopTest around your future method call. stopTest will ensure your future method was called.
  2. Create a class which will implement HttpCalloutMock and will set a fake response for your callout testing. This is a good link to study the HttpCalloutMock implementation.

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.