【apex专题】为WebService写TestClass
2024年10月23日
文章浏览:40
为WebService写TestClass


比如我们有如下WebService代码

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


TestClass的写法如下

@isTest
private class TestNewWebService
{
  static testMethod void testNewWebService()
  {
      DemoApexWebService.AccountInfo acc = new DemoApexWebService.AccountInfo();
      acc.AccName = 'ABC';
      acc.AccNumber = 123;
      DemoApexWebService.createAccount( acc );
 
      Account[] a = [select AccountNumber from Account where AccountNumber = '123'];
      System.assertEquals(a.size(), 1);
  }
}


关注 收藏