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
Supriyo Ghosh 9Supriyo Ghosh 9 

Code coverage for static void method

Hello,

How to cover this portion.Please help with sample code.

@future (callout=true)
    Public static void smsCustomer1(Id tskId) {
        SMS_Settings__c smsS = [select id, Name, senderId__c,task_sms__c,Mobile_Number__c, ValueFirst_Username__c, ValueFirst_Password__c from SMS_Settings__c where name ='Proposal'] ; 
        Task tsk = [select id,Status,Proposal_Number__r.Account_Name__r.FirstName,Proposal_Number__r.Account_Name__r.LastName,Proposal_Number__r.Account_Name__r.PersonMobilePhone 
                    from Task where id=:tskId];
                                            
        String fromNo = smsS.senderId__c; //'PGFIIT';
        String toNo =  '91' + tsk.Proposal_Number__r.Account_Name__r.PersonMobilePhone; // '919790882125'; // 
        String Name = tsk.Proposal_Number__r.Account_Name__r.FirstName + ' '+ tsk.Proposal_Number__r.Account_Name__r.LastName; 
        String message = smss.task_sms__c.replace('CUSTNAME', Name);
        String xmlMessage = '<?xml version="1.0" encoding="ISO-8859-1"?>';
                xmlMessage += '<!DOCTYPE MESSAGE SYSTEM "http://127.0.0.1/psms/dtd/message.dtd" >';
                xmlMessage += '<MESSAGE><USER USERNAME="'+smss.ValueFirst_Username__c+'" PASSWORD="'+smss.ValueFirst_Password__c+'"/>';
                xmlMessage += '<SMS UDH="0" CODING="1" TEXT="'+message +'" PROPERTY="0" ID="'+tskId+'">';
                xmlMessage += '<ADDRESS FROM="'+ fromNo +'" TO="'+toNo +'" SEQ="1" TAG="Premium Payment" />';
                xmlMessage += '</SMS></MESSAGE>';
        String encodedUrl = 'http://api.myvaluefirst.com/psms/servlet/psms.Eservice2?action=send&data='+EncodingUtil.urlEncode(xmlMessage, 'UTF-8');
        String decodedUrl = EncodingUtil.urlDecode(encodedUrl, 'UTF-8');  system.debug(decodedUrl);
        Http h = new Http(); 
        HttpRequest req = new HttpRequest(); 
        req.setEndpoint(encodedUrl); 
        req.setMethod('GET');
        if (test.isRunningTest()!=true) {
            HttpResponse res = invokeWebService(h, req); handleWebServiceResponseCust(tskId);
            system.debug(res.getBody());
        } else {
            handleWebServiceResponseCust(tskId);
        }
Best Answer chosen by Supriyo Ghosh 9
Sanjay Bhati 95Sanjay Bhati 95
Hi Supriyo Ghosh 9,

Create one setup method using @Testsetup annotation and insert all the needed data in that method. Now create your method with @isTest annotation and query the data you will get the data inserted in the setup method. 
If you are using this in trigger then you have to cover the code that is calling this method it will automatically cover up this method also.

Like below code
@TestSetup
public static void setupMethod()
{
    Task tsk = new Task();
    tsk.Subject = 'test';
    insert tsk;
}

@isTest
public static testMethod void method1()()
{
    List<Task> taskList = [Select Id,Subject From Task];
    // here you will get 1 record of Task that we inserted in setup method
    if(taskList.size() > 0){
       ControllerName.smsCustomer1(taskList[0].Id);
    }
}

 

All Answers

Sanjay Bhati 95Sanjay Bhati 95
Hi Supriyo Ghosh 9,

Create one setup method using @Testsetup annotation and insert all the needed data in that method. Now create your method with @isTest annotation and query the data you will get the data inserted in the setup method. 
If you are using this in trigger then you have to cover the code that is calling this method it will automatically cover up this method also.

Like below code
@TestSetup
public static void setupMethod()
{
    Task tsk = new Task();
    tsk.Subject = 'test';
    insert tsk;
}

@isTest
public static testMethod void method1()()
{
    List<Task> taskList = [Select Id,Subject From Task];
    // here you will get 1 record of Task that we inserted in setup method
    if(taskList.size() > 0){
       ControllerName.smsCustomer1(taskList[0].Id);
    }
}

 
This was selected as the best answer
Supriyo Ghosh 9Supriyo Ghosh 9
@sanjay thanks a ton.