function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rohan TelangRohan Telang 

Hello Developers, I want to write TEST class for POST method. Please help me.

I have written RESTAPI on Account and Contact object. There is POST method. I am passing JSON data from POSTMAN and it's getting inserted in Account object and it's related Contact.

Endpoint is-  "https://egssolutions--developer.my.salesforce.com/services/apexrest/APIForWebTeam/"

I wanted to write the TEST class. Can anybody help me?

Main Class-

@RestResource(urlMapping='/APIForWebTeam/*')
global class EGS_APIForWebTeam {
    
    public class AccountData{
      //public Id accountId;
        public String customer_code;
        public String web_customer_id;
        public String web_customer_mapping_id;
        public String account_name;
        public String account_source;
        public String email;
        public String email_pec;
        public String vat_number;
        public String social_security_number;
        public String phone;
        public String mobile_phone;
        public String type;
      //public String billingAddress;
        public String billing_street_address;
        public String billing_city;
        public String billing_state;
        public String billing_zipcode;
        public String billing_country;
      //public String shippingAddress;
        public String shipping_street_address;
        public String shipping_city;
        public String shipping_state;
        public String shipping_zipcode;
        public String shipping_country;
        public String product_category;
        public String product_subcategory;
      //public String country;
      //public String stateProvince;
      //public String country1;
        public String first_name;
        public String last_name;
    }
    
    @httpPost
    global static void createAccountData(){
        try{
            RestRequest req = RestContext.request;
            String jsonString = req.requestBody.tostring();
            List<AccountData> receivedData= (List<AccountData>)System.JSON.deserialize(jsonString, List<AccountData>.class);
            
            List<Account> accountList = new List<Account>();
            List<Contact> conList=New List<Contact>();
            
            for(AccountData accountData : receivedData){
                Account acc=New Account();
                
                acc.Name=accountData.account_name;
                acc.Customer_Code__c=accountData.customer_code;
                acc.AccountSource=accountData.account_source;
                acc.Email__c=accountData.email;
                acc.Email_PEC__c=accountData.email_pec;
                acc.VAT_number__c=accountData.vat_number;
                acc.Social_security_number__c=accountData.social_security_number;
                acc.Phone=accountData.phone;
                acc.Mobile_phone__c=accountData.mobile_phone;
                acc.Type=accountData.type;
              //acc.BillingAddress=accountData.billingAddress;
                acc.BillingStreet=accountData.billing_street_address;
                acc.BillingCity=accountData.billing_city;
                acc.BillingState=accountData.billing_state;
                acc.BillingPostalCode=accountData.billing_zipcode;
                acc.BillingCountry=accountData.billing_country;
              //acc.Country_C__c=accountData.country;
              //acc.State_province__c=accountData.stateProvince;
              //acc.ShippingAddress=accountData.shippingAddress;
                acc.ShippingStreet=accountData.shipping_street_address;
                acc.web_customer_id__c=accountData.web_customer_id;
                acc.web_customer_mapping_id__c=accountData.web_customer_mapping_id;
                acc.ShippingCity=accountData.shipping_city;
                acc.ShippingState=accountData.shipping_state;
                acc.ShippingPostalCode=accountData.shipping_zipcode;
                acc.ShippingCountry=accountData.shipping_country;
                acc.Product_Category__c=accountData.product_category;
                acc.Product_Subcategory__c=accountData.product_subcategory;
                
              //acc.Country__c=accountData.country;
                accountList.add(acc);
                
                
            }
            if(!accountList.isEmpty()){
            insert accountList;
            }
            
            for(AccountData accountData : receivedData){
                Contact varCon = New Contact();
                varCon.FirstName = accountData.first_name;
                varCon.LastName = accountData.last_name;
                varCon.Email = accountData.email;  
                varCon.MobilePhone = accountData.mobile_phone;
                varCon.Phone = accountData.phone;
                varCon.AccountId = accountList[0].id;
                conList.add(varCon);
            
            }
            
            if(!conList.isEmpty()){
            insert conList;    
            }
            
            RestResponse res = RestContext.response;
            res.statusCode = 200;
            String webCustomerId = accountList[0].web_customer_id__c;
            String webCustomerMappingId = accountList[0].web_customer_mapping_id__c;
            String customerCode = accountList[0].Customer_Code__c;
            res.responseBody= Blob.valueOf('Web Customer Id = '+webCustomerId+',  Salesforce Account Id = '+accountList[0].id+', Web Customer Mapping Id = '+webCustomerMappingId+', Customer Code = '+customerCode);
        }catch(Exception e){
            RestResponse res = RestContext.response;
            res.statusCode = 404;
            res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage());
        }
     }
  }




 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Rohan,

Can please refer the link for  REST test class.

https://developer.salesforce.com/forums/?id=9060G0000005PPvQAM

If this helps, please mark it as best answer

Thank you!!

 
Rohan TelangRohan Telang
Thank you for your response-

I referred the Test Class corrected by "Angelina Muddamalle" in below link and it increased the Test Class coverage of Mine.
https://developer.salesforce.com/forums/?id=9060G000000XdDoQAK
Rohan TelangRohan Telang
Please refer below "Test Class" for POST method for my above class-

@isTest
private class EGS_APIForWebTeamTestClass {
    
    @isTest
    static void testCreateAccountContact(){
        
        String JSONMsg ='[{"account_name":"testAPI27","account_source":"website","type":"customer","product_category":"dental","product_subcategory":"dental laboratory","first_name":"test5Con","last_name":"test5telang","email":"rohan5@gmail.com","web_customer_id":"abc5","web_customer_mapping_id":"xyz5","customer_code":"abcxyz5"}]';
           
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        
        req.requestURI = 'https://egssolutions--developer.lightning.force.com/services/apexrest/APIForWebTeam/';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JSONMsg);
        
        RestContext.request = req;
        RestContext.response= res;
        
        Test.startTest();
            EGS_APIForWebTeam.createAccountData();
        Test.StopTest();   
        
    }
    }