您的位置:首页 > 其它

在Salesforce中编写Unit Test

2013-11-12 10:04 302 查看
Unit Test 也是一个 Class 文件,所以在创建 Unit Test 的时候要选择 Test Class 类型来创建,请看如下截图(在Eclipse中):

@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: