title: Salesforce: An APEX test class for a web service callout
body: <p>I've been trying to figure out the test class for a web service callout. I have the four different classes. <code>API_Member</code> which has all the parameters with all the information and matching of the fields. I have a <code>API_Response</code> class and I've got an <code>AccountTrigger</code> and <code>AccountTriggerHandler</code>.</p>
  <p>In the <code>AccountTriggerHandler</code>, the whole HTTPRequest and callout is handled where I call the <code>API_Member</code> class as well and then triggers it from the <code>AccountTrigger</code>.</p>
  <p>Now the difficulty is the test class since I'm not an experienced developer.</p>
  <p>How would I manage to handle the test class for this code (I've made changes in case it's some important information)?</p>
  <pre><code>public with sharing class AccountTriggerHandler {
  
      public void isAfterInsert(list&lt;Account&gt; accountNew) {
          for(Account a: accountNew) {
              sendData(a.Id);
          }
      }
  
      public void isAfterUpdate(list&lt;Account&gt; accountNew) {
  
      }
  
      @future(callout=true)
      public static void sendData(string accountId) {
          API_Member api = new API_Member(accountId);
          api.setupData();
          API_Member.Data data = api.dataList;
          string json = JSON.serialize(data); 
          system.debug('JSON' + json);
          Http http = new Http();
          HttpRequest request = new HttpRequest();
          string url = 'http://api.XXXXXXXXXXXXXXXXXXXXXXX';
          string username = 'Ext-XXXXXXXXXXXXXXX-XXXXXXXXXXXXXX';
          string password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
          Blob headerValue = Blob.valueOf(username + ':' + password);
          String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
          request.setHeader('Authorization', authorizationHeader); 
          request.setHeader('Content-Type', 'application/json;charset=UTF-8');
          request.setEndpoint(url);
          request.setMethod('POST');
          request.setBody(json);
          HttpResponse response = http.send(request);
          if (response.getStatusCode() == 201 || response.getStatusCode() == 200 ) {
              API_Response responseCTRL = (API_Response) System.JSON.deserialize(response.getBody(), API_Response.class);
              list&lt;API_Response.Meta&gt; m = responseCTRL.Meta;
              System.debug('recordID='+m[0].recordID);
              api.setResponse(m[0].recordID,response.getStatus());
          }
      }
  }
  </code></pre>
  <p><strong>Here is the API_Member Class</strong></p>
  <pre><code>public class API_Member {
      public Data dataList;
      public Account input;
      public Contact c;
      public class Data {
          public list&lt;Info&gt; data;
      }
      public class Info {
              public String active;
              public String address_city;
              public String address_country;
              public String address_line_1;
              public String address_line_2;
              public String address_state;
              public String address_zipcode;
              public String company_name;
              public String date_end;
              public String date_paid;
              public String date_start;
              public String email;
              public String membership_number;
              public String mobile;
              public String org_number;
              public String person_first_name;
              public String person_last_name;
              public String person_number;
              public String phone;
              public String status;
              public String web_address;
      }
  
      public API_Member(string accountId) {
          this.input = getAccount(accountId);
          this.c = getContact();
      }
  
      public void setupData(){
          dataList = new Data();
          Info data = new Info();
          data.active = 'true'; //To be verified
          data.address_city = input.Ort__c;
          data.address_country = '';
          data.address_line_1 = input.Adress1__c;
          data.address_line_2 = '';
          //data.address_state = input.L_n_Namn__c;
          data.address_zipcode = input.Postnummer__c;
          data.company_name = input.Name;
          data.date_end = input.Expiry_Date__c != null ? string.valueOf(input.Expiry_Date__c) : '';
          data.date_paid = input.Last_Renew__c != null ? string.valueOf(input.Last_Renew__c) : '';
          data.date_start = input.Last_Renew__c != null ? string.valueOf(input.Last_Renew__c): '';
          data.email = input.Medlems_Epost__c;
          data.membership_number = input.AccountNumber;
          data.mobile = input.Phone;
          data.org_number = input.Organisationsnummer__c;
          data.person_first_name = c != null ? c.FirstName : '';
          data.person_last_name = c != null ? c.LastName : '';
          data.person_number = c != null ? c.Personnummer__c : '';
          data.phone = input.Phone;
          data.status = input.Status_Pay__c;
          data.web_address = input.Website;
  
          dataList.data = new list&lt;Info&gt;{data};
      }
  
      public Contact getContact(){
          list&lt;Contact&gt; c = [ SELECT Id,
                              Personnummer__c,
                              LastName,
                              FirstName
                              FROM Contact
                              WHERE AccountId =: input.Id
                              ORDER BY CreatedDate
                              ASC
                              LIMIT 1
          ];
          if(c.size() &gt; 0){
              return c[0];
          }
          return null;
      }
  
      public Account getAccount(string accountId){
          Account a = [SELECT Id,
                      Ort__c,
                      Adress1__c,
                      Postnummer__c,
                      Name,
                      Expiry_Date__c,
                      Last_Renew__c,
                      Medlems_Epost__c,
                      AccountNumber,
                      Phone,
                      Organisationsnummer__c,
                      Status_Pay__c,
                      F_RecordId__c,
                      F_Satus__c,
                      Website
                      FROM Account
                      WHERE Id =: accountId
                      LIMIT 1
          ];
          return a;
      }
      
      public void setResponse(string recordId, string Status) {
          input.F_RecordId__c  = recordId;
          input.F_Satus__c  = status;
          update input;
      }
  }
  </code></pre>
  <p><strong>Here is the API_Response Class</strong></p>
  <pre><code>public class API_Response {
  
      public class Meta {
          public String recordID {get;set;} 
          public String href {get;set;} 
      }
      
      public List&lt;Meta&gt; meta {get;set;} 
      public List&lt;Data&gt; data {get;set;} 
      public Info info {get;set;} 
      public List&lt;MetaField&gt; metaField {get;set;} 
      
      public class MetaField {
          public String name {get;set;} 
          public Integer autoEntered {get;set;} 
          public Integer global_Z {get;set;} // in json: global
          public Integer maxRepeat {get;set;} 
          public String resultType {get;set;}         
      }
      
      public class Data {
          public String address_city {get;set;} 
          public String address_country {get;set;} 
          public String address_line_1 {get;set;} 
          public String address_line_2 {get;set;} 
          public String address_state {get;set;} 
          public String address_zipcode {get;set;} 
          public String company_name {get;set;} 
          public String date_end {get;set;} 
          public String date_start {get;set;} 
          public String email {get;set;} 
          public String membership_number {get;set;} 
          public String mobile {get;set;} 
          public String org_number {get;set;} 
          public String person_first_name {get;set;} 
          public String person_last_name {get;set;} 
          public String person_number {get;set;} 
          public String phone {get;set;} 
          public String status {get;set;} 
          public String upload_data_status {get;set;} 
          public String web_address {get;set;} 
  
      }
      
      public class Info {
          public String X_RESTfm_Version {get;set;} // in json: X-RESTfm-Version
          public String X_RESTfm_Protocol {get;set;} // in json: X-RESTfm-Protocol
          public Integer X_RESTfm_Status {get;set;} // in json: X-RESTfm-Status
          public String X_RESTfm_Reason {get;set;} // in json: X-RESTfm-Reason
          public String X_RESTfm_Method {get;set;} // in json: X-RESTfm-Method
          public String X_RESTfm_PHP_memory_limit {get;set;} // in json: X-RESTfm-PHP-memory_limit
          public String X_RESTfm_PHP_post_max_size {get;set;} // in json: X-RESTfm-PHP-post_max_size
  
      }
      
  }
  </code></pre>