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
logontokartiklogontokartik 

Testing APEX Web services

 

Hello,
I am trying to write a APEX Webservice that will be called from .NET. Before trying to use it at NET i wanted to test the WS Class I wrote in the APEX using a test class. Does anyone have examples of any test classes to test the APEX Web services?
Appreciate your response.
Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

You can simply invoke the method that you've annotated as 'webservice' in your test class. So something like this:

 

Say your Apex web service is this:

 

global class SampleApexWebSvc {
    global class AccountInfo {
        WebService String AcctName;
        WebService Integer AcctNumber;
    }
    
    WebService static Account createAccount(AccountInfo info) {
        Account acct = new Account();
        acct.Name = info.AcctName;
        acct.AccountNumber = String.valueOf(info.AcctNumber);
        insert acct;
        return acct;
    }

}

 

 

You can test the above with the following test class:

 

 

@isTest
private class testMyWebService
{
  
  
  static testMethod void testMyWebSvc()
  {
      SampleApexWebSvc.AccountInfo act = new SampleApexWebSvc.AccountInfo();
      act.AcctName = 'test';
      act.AcctNumber = 123;
      SampleApexWebSvc.createAccount( act );
      
      Account[] a = [select AccountNumber from Account where AccountNumber = '123'];
      System.assertEquals(a.size(), 1);
  }
}

 

Hope this helps...

All Answers

mtbclimbermtbclimber

Testing methods in apex that are annotated as webservices should be no different from testing any other static method within apex and there are plenty of examples of testing you can look at. 

 

This introduction is a good place to start. And the testing chapter in the Apex documentation is another great resource.

forecast_is_cloudyforecast_is_cloudy

You can simply invoke the method that you've annotated as 'webservice' in your test class. So something like this:

 

Say your Apex web service is this:

 

global class SampleApexWebSvc {
    global class AccountInfo {
        WebService String AcctName;
        WebService Integer AcctNumber;
    }
    
    WebService static Account createAccount(AccountInfo info) {
        Account acct = new Account();
        acct.Name = info.AcctName;
        acct.AccountNumber = String.valueOf(info.AcctNumber);
        insert acct;
        return acct;
    }

}

 

 

You can test the above with the following test class:

 

 

@isTest
private class testMyWebService
{
  
  
  static testMethod void testMyWebSvc()
  {
      SampleApexWebSvc.AccountInfo act = new SampleApexWebSvc.AccountInfo();
      act.AcctName = 'test';
      act.AcctNumber = 123;
      SampleApexWebSvc.createAccount( act );
      
      Account[] a = [select AccountNumber from Account where AccountNumber = '123'];
      System.assertEquals(a.size(), 1);
  }
}

 

Hope this helps...

This was selected as the best answer
logontokartiklogontokartik

Thank you Admin,

 

This did help me. I was thinking if there was any special way of testing it. But this is enough for me to get the code coverage.

 

Regards

 

PrashantKPrashantK

Hi, Please tell me how can I increase Code coverage of following service. 

 

global class ContactServiceInformation
{
webService static void Event(String Id,Integer yy,Integer mm,Integer dd,String sub,String description)
{
Task task = new Task();
task.WhoId = Id;
task.Subject = sub;
task.priority= 'Normal';
task.status = 'Completed';
task.description = description;
task.ActivityDate=date.newInstance(yy,mm,dd);
insert task;

}

webService static String save(String companyName,Integer numEmployees,String streetAddress,String cityAddress,
String stateAddress,String postalCodeAddress,String countryAddress,String department,String phone,
String firstName,String lastName,String title,String email) {
{
String fname=firstName;
String lname=lastName;
Account a = new Account(
Name = companyName,
NumberOfEmployees = numEmployees,
ShippingStreet = streetAddress,
ShippingCity = cityAddress,
ShippingState = stateAddress,
ShippingPostalCode = postalCodeAddress,
ShippingCountry = countryAddress);

insert a;
String AccId=a.Id;

Contact c = new Contact(
AccountId=a.Id,
FirstName = firstName,
LastName = lastName,
Account = a,
Department = department,

Email = email,
Phone=phone,
Title = title,
MailingStreet = streetAddress,
MailingCity = cityAddress,
MailingState = stateAddress,
MailingPostalCode = postalCodeAddress,
MailingCountry = countryAddress);

insert c;

Contact ct = [select Id from Contact where AccountId=:AccId and FirstName=:fname and LastName= :lname and Email=:email and Phone=:phone];
return string.valueof(ct.Id);

}
}
}

 

 

I have created following testing methods which showing only 15% of code coverage. 

 

@isTest
private class testMyWebService
{
  
  static testMethod void testMyWebSvc()
  {      
      test.startTest();    
      ContactServiceInformation.Event('1',13,06,09,'Teaching Notes','Teaching notes reading');                           
      String myvalue = ContactServiceInformation.save('workmethods inc',1,'123 pune','Pune','Maharashtra','411046','India','Computer org','89489499','Prashant','Khadatkar','Software','prashantkhadatkar@yahoo.com');
      Contact ct = [select FirstName from  Contact where FirstName='Prashant'  and LastName='Khadatkar'  and Email='prashantkhadatkar@yahoo.com'];                   
      test.stopTest();      
      

  }
}

 

 

Need Quick response. Please help out. thanks in advance.

Amitkumar1234Amitkumar1234

HI Prashnat ,

 

Please see below solution , sorry for late reply.

 

@isTest
private class testMyWebService
{

static testMethod void testMyWebSvc()
{
test.startTest();

Contact who = new Contact(LastName='Salesforce Contact');
insert who;

//ContactServiceInformation.Event('1',13,10,10,'Teaching Notes','Teaching notes reading');
//Insted of passing some string pass actual who id
//WhoID refers to people things. So that would be typically a Lead ID or a Contact ID

Date d = system.today();
ContactServiceInformation.Event(who.Id,d.year(),d.month(),d.day(),'Teaching Notes','Teaching notes reading');

String myvalue = ContactServiceInformation.save('workmethods inc',1,'123 pune','Pune','Maharashtra','411046','India','Computer org','89489499','Prashant','Khadatkar','Software','prashantkhadatkar@yahoo.com');
Contact ct = [select FirstName from Contact where FirstName='Prashant' and LastName='Khadatkar' and Email='prashantkhadatkar@yahoo.com'];
test.stopTest();

}
}

 

Thnaks.