1

Below is my code which class is call from php may i know how can i test my webservice for code coverage

global class SpecialAccounts {

  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;
  }

        Id [] results = new Id[3];
        results[0] = parent.Id;
        results[1] = child.Id;
        results[2] = grandChild.Id;
        return results;
    }
}

2 Answers 2

1

You just have to call your static class in test class with proper data

List<Main.AccountWrapper> lstmainwrap=new List<Main.AccountWrapper>();
integer i;
 for(i=0;i<=10;i++){
    Main.AccountWrapper accwarp=new Main.AccountWrapper();
    accwarp.accName='testacc'+i;
    accwarp.accNumber=i;
    lstmainwrap.add(accwarp);
 }

Main.RequestClass req=new Main.RequestClass();
req.accounts=lstmainwrap;

Test.startTest();
  Main.ResponseClass RES=Main.behaviourOfWebService(req);
Test.stopTest();

//Assert the behaviour as expected

6
  • when i use your test class records are not inserting in sfdc.May i know why it happens.
    – Sathya
    Commented Dec 20, 2013 at 10:20
  • test.startTest() and stoptest it showing an error method does not exist.
    – Sathya
    Commented Dec 20, 2013 at 10:21
  • post what ave you tried .This was a sample code ,so post your code so far. Commented Dec 20, 2013 at 10:26
  • i post my code in answer coloumn once check it.
    – Sathya
    Commented Dec 20, 2013 at 10:32
  • Again records are not inserting i think we miss the insert statement.Could you modify my code.
    – Sathya
    Commented Dec 20, 2013 at 10:58
0

Since you are hosting the webservices inside Salesforce, it shouldn't be very difficult to test them. Just call the methods of the webservice class like they are some normal methods and test/assert the outcome.

For instance you can call your "behaviourOfWebService" method by just doing

Main.behaviourOfWebService(new Main.RequestClass());
//plz send some actuall data instead of just "new Main.RequestClass()"

Have a look at the below links to read more about how to write test classes

3
  • Can you give some sample code..
    – Sathya
    Commented Dec 20, 2013 at 8:10
  • @Sathya I gave you the line to invoke the webservice method. It shouldnt be any different than writing test class for any other apex class
    – Avidev9
    Commented Dec 20, 2013 at 8:12
  • but it showing an error account name is not exist Main.behaviourOfWebService(new MainClass4.RequestClass()); act.accName = 'Mytest'; act.accNumber = '1238'; Main.behaviourOfWebService( act );
    – Sathya
    Commented Dec 20, 2013 at 8:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .