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
sailersailer 

Test Cases for Webservice Call out

@RestResource(urlMapping='/account')
global class TestRestServices {
    @HttpGet
    global static List<Property_Obj__c> getAccounts() {
       List<Property_Obj__c> lst;
       try
       {
        RestRequest req=RestContext.request;
        String namevalue=req.params.get('name');
        lst=[SELECT Send_From_Email__c,Listing_Sales_Associate__c,Buyer_0__c,Listing_Sales_Associate_Email__c,Seller_0__c,Listing_Broker__c,Address__c,Comm_B__c,County__c,Comm_S__c,
                Tax_Id__c,Coop_Sales_Assoc__c,Legal_Description_0__c,Offer_Date__c,PURCHACE_PRICE__c,HOA_Coa_Fee__c,Initial_Deposit__c,HOA_Coa_Freq__c,
                BALANCE_TOCLOSE__c,Terms_Num_Val__c,EXPIRATION_Date__c,Annual_Fee__c,Days_To_Close_After_Acceptance__c,city__c,Escrow_Name__c,zip__c,Escrow_Address__c,SS_Date2__c,
                Escrow_Phone__c,C_Date3__c,Escrow_Email__c,C2_Date4__c,Email_Fax__c,SS_Buyer2__c,INSPECT_PERIOD__c,C_Buyer3__c,OTHER_LINE_0__c,SS_Seller2__c,
                OTHER_LINE_2__c,C_Seller3__c,OTHER_LINE_3__c,SS_LD2__c,C_LD__c FROM Property_Obj__c where Status__c='Forward To E2P'];
         for(Property_Obj__c Acc:lst)
        {

              if(lst.size()>0)
              {
                 //Acc.Id=Id;
                 // Acc.Status__c ='Sent To E2P';
              }
           update Acc;
           }
        return lst;
       }catch(Exception e)
       {
       system.debug('Error'+e.getMessage());
       }
       return lst;
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Satyendra RawatSatyendra Rawat

Hi Sailer,

 

I have try our end, here is working fine,

 

Please edit services class and save it ,

 

then save the testclass .

All Answers

Satyendra RawatSatyendra Rawat

Hi Sailer,

just you want test method for the code coverage, you create proper data for Property_Obj__c object and  

"getAccounts" is a static method, you can call directly TestRestServices .getAccounts();

 

your problem is that how to set parameter, simple create a static variable for the test method and and check the isRunningTest() == true then set the value to "namevalue" , and remains same.

 

if you want to consume web services,You can consume outside of this instance.

 

If you want to more reply me.

sailersailer

Hi Satya,

 

I worte this code but its not working

 

@isTest
private class TestClasscallout {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        List<Property_Obj__c> lst;
        test.startTest();
        myUnitTest()
        test.stopTest();
    }
}

 

 

Can you please help me out.

 

Regards

Sailer

Satyendra RawatSatyendra Rawat

Hi Sailer,

if it is not working i will help for writing code.

@isTest
private class TestClasscallout {

static testMethod void myUnitTest() {
// TO DO: implement unit test

test.startTest();
Property_Obj__c property = new Property_Obj__c();
property.Send_From_Email__c = 'test@gmail.com';
property.Listing_Sales_Associate_Email__c = 'test@gmail.com';
property.Status__c = 'Forward To E2P';
//before insert set the all mandatry field with value like above
insert property;
List<Property_Obj__c> lstProp= TestRestServices.getAccounts();
test.stopTest();
}
}

 

Hit the Kudos button (star) if it post helps you

digamber.prasaddigamber.prasad

Hi,

 

Best way to write test class for an endpoint as per salesforce best practices is useing HttpCalloutMock. Please see below url:-

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_testing_httpcalloutmock.htm

 

Let me know if you have any specific question.

sailersailer

Hi Satya,

 

I am getting the following error .

 

 

Save error: Method does not exist or incorrect signature: TestRestServices.getAccounts()

 

 

@isTest
private class TestClasscallout {

static testMethod void myUnitTest()
 {
// TO DO: implement unit test

test.startTest();
Property_Obj__c property = new Property_Obj__c();
property.Send_From_Email__c = 'test@gmail.com';
property.Listing_Sales_Associate_Email__c = 'test@gmail.com';
property.Status__c = 'Forward To E2P';
//before insert set the all mandatry field with value like above
//sert property;
//List<Property_Obj__c> lstProp= TestRestServices.getAccounts();
List<Property_Obj__c> lst= TestRestServices.getAccounts();
test.stopTest();
}
}

Regards

Sailer

Satyendra RawatSatyendra Rawat

Hi Sailer,

 

I have try our end, here is working fine,

 

Please edit services class and save it ,

 

then save the testclass .

This was selected as the best answer