在Salesforce中编写Unit Test
2013-11-12 10:04
302 查看
Unit Test 也是一个 Class 文件,所以在创建 Unit Test 的时候要选择 Test Class 类型来创建,请看如下截图(在Eclipse中):
View Code
更多详细信息请看如下链接:
http://andyinthecloud.com/2013/05/11/code-coverage-for-wsdl2apex-generated-classes/ http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm
如何对Salesforce提供的Restful Service所对应的Apex Class 进行 Unit Test, 请看如下链接:
http://salesforce.stackexchange.com/questions/4988/writing-test-classes-for-apex-restservice
还有一种偷懒的方式 请看: http://sfdc.arrowpointe.com/2009/05/01/testing-http-callouts/
如何对调用External Restful Service的Apex Class 进行 Unit Test, 请看如下链接:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_testing_httpcalloutmock.htm
@isTest private class CreditLimitControllerTest { static testMethod void myUnitTest() { // query for a test user User u = [select id from User where id =: UserInfo.getUserId() limit 1]; system.assert(u.id != null); // insert test data Account account001 = new Account( Name = 'Test Account 0101', AccountNumber = 'IN1200047', Credit_Type__c = 'Account' ); insert account001; system.assert(account001.id != null); // run test as current test user system.runAs(u){ // start test Test.startTest(); // This causes a fake response to be generated Test.setMock(WebServiceMock.class, new WebServiceMockImpl()); Test.setCurrentPage(Page.CreditLimit); ApexPages.StandardController sc = new ApexPages.StandardController(account001); CreditLimitController con = new CreditLimitController(sc); // test the constructor contains multiple parameters CreditLimitController.CreditLimitInfo testCLI = new CreditLimitController.CreditLimitInfo('111', 1111.00, 1111.00, 1111.00); // end test Test.stopTest(); } } private class WebServiceMockImpl implements WebServiceMock { public void doInvoke( Object stub, Object request, Map<String, Object> response, String endpoint, String soapAction, String requestName, String responseNS, String responseName, String responseType) { if(request instanceof ITVDataFeedService.Feed_element){ // generate the fake data to test ITVDataFeedService.FeedResponse_element feedRes = new ITVDataFeedService.FeedResponse_element(); ITVDataFeedService.ArrayOfArrayOfNV aaOfNV = new ITVDataFeedService.ArrayOfArrayOfNV(); ITVDataFeedService.ArrayOfNV aOfNV = new ITVDataFeedService.ArrayOfNV(); ITVDataFeedService.NV nvCustomerID = new ITVDataFeedService.NV(); nvCustomerID.N = 'CustomerID'; nvCustomerID.V = 'IN1200047'; ITVDataFeedService.NV nvCreditLimit = new ITVDataFeedService.NV(); nvCreditLimit.N = 'CreditLimit'; nvCreditLimit.V = '1000.11'; ITVDataFeedService.NV nvAvailableCredit = new ITVDataFeedService.NV(); nvAvailableCredit.N = 'AvailableCredit'; nvAvailableCredit.V = '1000.11'; ITVDataFeedService.NV nvBalance = new ITVDataFeedService.NV(); nvBalance.N = 'Balance'; nvBalance.V = '1000.11'; ITVDataFeedService.NV[] nvArray = new ITVDataFeedService.NV[] { nvCustomerID, nvCreditLimit, nvAvailableCredit, nvBalance }; aOfNV.NV = nvArray; ITVDataFeedService.ArrayOfNV[] aOfNVArray = new ITVDataFeedService.ArrayOfNV[] { aOfNV }; aaOfNV.ArrayOfNV = aOfNVArray; feedRes.FeedResult = aaOfNV; // set the fake data to response response.put('response_x', feedRes); } } } }
View Code
更多详细信息请看如下链接:
http://andyinthecloud.com/2013/05/11/code-coverage-for-wsdl2apex-generated-classes/ http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm
如何对Salesforce提供的Restful Service所对应的Apex Class 进行 Unit Test, 请看如下链接:
http://salesforce.stackexchange.com/questions/4988/writing-test-classes-for-apex-restservice
还有一种偷懒的方式 请看: http://sfdc.arrowpointe.com/2009/05/01/testing-http-callouts/
如何对调用External Restful Service的Apex Class 进行 Unit Test, 请看如下链接:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_testing_httpcalloutmock.htm
相关文章推荐
- 编写unit test以及自动化测试WebDriver
- Struts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
- 22.七-通过unittest单元测试框架编写单元测试用例
- salesforce------Unit Test Considerations
- (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”); (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
- Python利用unittest框架编写接口测试小栗子
- selenium-python编写unittest运行代码时候不执行
- python实例编写(6)--引入unittest测试框架,构造测试集批量测试(以微信统一管理平台为例)
- 用unittest框架编写登录博客园的案例(登陆成功和登陆失败)
- Struts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
- C++ UnitTest编写
- python unittest编写测试代码
- 使用简单的unittest编写接口测试脚本
- Salesforce中为服务器分页Pagination编写测试类-Test Class For ApexPages.StandardSetController, hasNext, next etc.
- ts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
- ABAP Unit Test
- selenium之unittest的简单用法(一)
- python-unittest(7)
- dotCover 2.0增添对Integrated Unit Test Runner与Visual Studio 2012的支持
- Code UI Test -- 手动编写Coded UI Test(existing browser)