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
Sateesh KumarSateesh Kumar 

Hi CAN ANY ONE HELP ME WITH TEST COVERAGE FOR WEB SERVICES CLASS. i cant cover the total code. just 11% of code is covered by test class.. i want it to cover minimum 75%

Class:
_______________----
public with sharing class SendSMS
{
public void run(String subject,String msg,string smsNumber)
    {
            SessionService S=new SessionService();
            SessionService.SessionServiceSoap sa=new  SessionService.SessionServiceSoap();
             
                sessionservice.BindingResult BinidingResult=Sa.GetBindings(m_Username );
                System.debug('BinidingResult>>>'+BinidingResult.sessionServiceLocation);
                sa.endpoint_x = BinidingResult.sessionServiceLocation;
                // Sa.GetServiceInformation('Salesforce');
                // sa.endpoint_x = 'https://na2.esker.com/EDPWS_C/EDPWS.dll?BindingId=20130730093630andrew.luu@tunstall.com&Handler=Default';
                sessionservice.LoginResult loginResult=sa.Login(m_Username , m_Password );
                String SessinID=loginResult.sessionID;
                submissionservice.SubmissionServiceSoap Submission=new submissionservice.SubmissionServiceSoap();
                Submission.endpoint_x = BinidingResult.SubmissionServiceLocation;
                submissionservice.SessionHeader Header=new submissionservice.SessionHeader();
                Header.SessionID=SessinID;
                Submission.SessionHeaderValue= Header;
                System.debug(Submission.endpoint_x+'********* SessinID ************* '+SessinID);
                // Now allocate a transport with transportName = "SMS"
                    submissionservice.Transport transport = new submissionservice.Transport();
                    transport.RecipientType='';
                    transport.TransportIndex='';
                    transport.TransportName='SMS';
                  
                    submissionservice.vars_element vrs1=new  submissionservice.vars_element();
                    List<submissionservice.Var> VarList=new  List<submissionservice.Var>();
                    submissionservice.Var Vars=new submissionservice.Var();
                    Vars.Attribute = 'Subject';
                   // Vars.type_x='1';
                    Vars.simpleValue = subject;
                   // Vars.nValues=4;
                  
                    submissionservice.Var Vars1=new submissionservice.Var();
                    Vars1.Attribute = 'FromName';
                    // Vars1.type_x='1';
                    Vars1.simpleValue = 'TUNSTALL';
                    // Vars1.nValues=4;
                  
                    submissionservice.Var Vars2=new submissionservice.Var();
                    Vars2.Attribute = 'SMSNumber';
                    // Vars2.type_x='1';'216-256-8216'
                    Vars2.simpleValue = smsNumber;     
                    // Vars2.nValues=4;
                  
                    submissionservice.Var Vars3=new submissionservice.Var();
                    Vars3.Attribute = 'Message';
                    // Vars3.type_x='1';
                    Vars3.simpleValue = msg;
                    // Vars3.nValues=4;
                  
                    VarList.add(Vars);
                    VarList.add(Vars1);
                    VarList.add(Vars2);
                    VarList.add(Vars3);
                    vrs1.Var=VarList;
                    transport.Vars=vrs1;
                    System.debug('######### Mahesh ############# '+submission.submitTransport(transport));
          
  
    }
}

@Test Class
public class SendSMS_TC
{
    static testmethod void m1()
    {
        SendSMS ss = new SendSMS();
        ss.run('test data','test','678jjh');
    }
}
Best Answer chosen by Sateesh Kumar
Gonzalo AbrunaGonzalo Abruna
Of course, the code after if(!Test.isRunningTest()) will never be covered, because you are saying to your Testclass that it should not go within the if sentence.
Instead of that, try to apply some of the things that I suggested in my previous posts, for example, try to move if(!Test.isRunningTest()) below, doing something like replacing sessionservice.LoginResult loginResult=sa.Login(m_Username , m_Password ); with:

sessionservice.LoginResult loginResult;
if(!Test.isRunningTest())
     loginResult=sa.Login(m_Username , m_Password );
else{
     loginResult=new sessionservice.LoginResult();
     loginResult.sessionID='1234';
}

As I said before, I am just assuming that your Web-Service call comes from sa.Login() method, which I am not sure. Just follow these tips and you will get to a solution.

All Answers

Gonzalo AbrunaGonzalo Abruna
Hello,

A good practice is to encapsulate in a method the piece of code that cannot be run in a test class. Also usage of Test.isRunningTest() method is very helpful, something like the following:

public void run(String subject, String msg, String smsNumber){
  //Do actions that can be invoked from Test Class
  //...
  if(!Test.isRunningTest())
   executeCallout();
  else{
   //Mimic the output with some expected result
  }
  //...
}

private void executeCallout(){
  //Do actions that cannot be invoked from Test Class
}
Sateesh KumarSateesh Kumar
Thank u Gonzalo Abruna. i had followed the same code what you had given above but the code coverage is only 11% can u please help me out to get minimum of 75% code coverage
Gonzalo AbrunaGonzalo Abruna
Hi,

it's very difficult to tell you what you have to change without seeing the lines that are covered and the method that triggers the web service, so let me tell you the steps that you should follow:

1) Find the lines of code that are not covered: use developer console to check this.
2) Identify where is the web-service invoked. Check that line and bypass it with the method I posted before. Example:

If web service was executed at this line:
sessionservice.LoginResult loginResult=sa.Login(m_Username , m_Password );

You should replace it by something like this:
if(!Test.isRunningTest()) //If we are not in a test execution, perform normal operation
          sessionservice.LoginResult loginResult=sa.Login(m_Username , m_Password );
else //createMyOwnLoginResult will be a new method that returns a LoginResult instance with some predefined values for testing
          sessionservice.LoginResult loginResult= createMyOwnLoginResult();

3) Run tests again and check that the lines that you expected to be covered are now covered.

Good luck,

Gonzalo.
 
Sateesh KumarSateesh Kumar
Hi Gonzalo Abruna
The code That i made bold got covered and the remaining code not got covered.
pls help me out.

Class:
_______________----
public with sharing class SendSMS
{
public void run(String subject,String msg,string smsNumber)
    {
   
            SessionService S=new SessionService();
            SessionService.SessionServiceSoap sa=new  SessionService.SessionServiceSoap();
            if(!Test.isRunningTest())
                {

                sessionservice.BindingResult BinidingResult=Sa.GetBindings(m_Username );
                System.debug('BinidingResult>>>'+BinidingResult.sessionServiceLocation);
                sa.endpoint_x = BinidingResult.sessionServiceLocation;
                // Sa.GetServiceInformation('Salesforce');
                // sa.endpoint_x = 'https://na2.esker.com/EDPWS_C/EDPWS.dll?BindingId=20130730093630andrew.luu@tunstall.com&Handler=Default';
                sessionservice.LoginResult loginResult=sa.Login(m_Username , m_Password );
                String SessinID=loginResult.sessionID;
                submissionservice.SubmissionServiceSoap Submission=new submissionservice.SubmissionServiceSoap();
                Submission.endpoint_x = BinidingResult.SubmissionServiceLocation;
                submissionservice.SessionHeader Header=new submissionservice.SessionHeader();
                Header.SessionID=SessinID;
                Submission.SessionHeaderValue= Header;
                System.debug(Submission.endpoint_x+'********* SessinID ************* '+SessinID);
                // Now allocate a transport with transportName = "SMS"
                    submissionservice.Transport transport = new submissionservice.Transport();
                    transport.RecipientType='';
                    transport.TransportIndex='';
                    transport.TransportName='SMS';
                 
                    submissionservice.vars_element vrs1=new  submissionservice.vars_element();
                    List<submissionservice.Var> VarList=new  List<submissionservice.Var>();
                    submissionservice.Var Vars=new submissionservice.Var();
                    Vars.Attribute = 'Subject';
                   // Vars.type_x='1';
                    Vars.simpleValue = subject;
                   // Vars.nValues=4;
                 
                    submissionservice.Var Vars1=new submissionservice.Var();
                    Vars1.Attribute = 'FromName';
                    // Vars1.type_x='1';
                    Vars1.simpleValue = 'TUNSTALL';
                    // Vars1.nValues=4;
                 
                    submissionservice.Var Vars2=new submissionservice.Var();
                    Vars2.Attribute = 'SMSNumber';
                    // Vars2.type_x='1';'216-256-8216'
                    Vars2.simpleValue = smsNumber;    
                    // Vars2.nValues=4;
                 
                    submissionservice.Var Vars3=new submissionservice.Var();
                    Vars3.Attribute = 'Message';
                    // Vars3.type_x='1';
                    Vars3.simpleValue = msg;
                    // Vars3.nValues=4;
                 
                    VarList.add(Vars);
                    VarList.add(Vars1);
                    VarList.add(Vars2);
                    VarList.add(Vars3);
                    vrs1.Var=VarList;
                    transport.Vars=vrs1;
                    System.debug('######### Mahesh ############# '+submission.submitTransport(transport));
         }
 
    }
}

@isTest
-----------------------
public class SendSMS_TC
{
    static testmethod void m1()
    {
        SendSMS ss = new SendSMS();
        ss.run('test data','test','678jjh');
    }
}



Gonzalo AbrunaGonzalo Abruna
Of course, the code after if(!Test.isRunningTest()) will never be covered, because you are saying to your Testclass that it should not go within the if sentence.
Instead of that, try to apply some of the things that I suggested in my previous posts, for example, try to move if(!Test.isRunningTest()) below, doing something like replacing sessionservice.LoginResult loginResult=sa.Login(m_Username , m_Password ); with:

sessionservice.LoginResult loginResult;
if(!Test.isRunningTest())
     loginResult=sa.Login(m_Username , m_Password );
else{
     loginResult=new sessionservice.LoginResult();
     loginResult.sessionID='1234';
}

As I said before, I am just assuming that your Web-Service call comes from sa.Login() method, which I am not sure. Just follow these tips and you will get to a solution.
This was selected as the best answer