Salesforce_ModeInGPM_2015.5.28总结
今日工作任务:
1. 更新客户代码合并提交到我们代码库
2. 理解feizhi补上的单元测试代码,针对MnPriceApprovalForm的
3. 额外有时间学习下单元测试的概念
2. 理解feizhi补上的单元测试代码,针对MnPriceApprovalForm的
3. 额外有时间学习下单元测试的概念
salesforce中apex代码的测试流程。
1.定义测试类,注明@isTest注解
@isTest public class MnPriceApprovalFormControllerTest { }
2.定义测试类中的测试数据类
@isTest public class MnPriceApprovalFormControllerTest { private static Integer APPROVAL_LEVELS_IN_THIS_TEST = 3; public class TestData { public User localUser1, regionUser1, globalUser1, submitterUser1, adminUser1; public MnCountry__c country; public MnProduct__c family, family2; public MnProductSKU__c product1, product2; public MnPrice__c productPricing1, productPricing2; public MnPriceType__c globalPriceLevel; public MnCountryPriceType__c priceLevel; public MnCountryProductSKU__c countryProductSKU1, countryProductSKU2; public TestData() { TestDataCreator.createUsers( new List<Map<String, Object>> { new Map<String, Object> { TestDataCreator.USER_USERNAME => 'LocalUser1@testorg.com', TestDataCreator.USER_LOCALE => 'en_US', TestDataCreator.PROFILE_NAME => 'GPM_Standard_User', TestDataCreator.PERMISSION_SET_NAME => 'GPM_FOUNDATION' }, new Map<String, Object> { TestDataCreator.USER_USERNAME => 'RegionUser1@testorg.com', TestDataCreator.USER_LOCALE => 'en_US', TestDataCreator.PROFILE_NAME => 'GPM_Standard_User', TestDataCreator.PERMISSION_SET_NAME => 'GPM_FOUNDATION' }, new Map<String, Object> { TestDataCreator.USER_USERNAME => 'GlobalUser1@testorg.com', TestDataCreator.USER_LOCALE => 'en_US', TestDataCreator.PROFILE_NAME => 'GPM_Standard_User', TestDataCreator.PERMISSION_SET_NAME => 'GPM_ADMIN' }, new Map<String, Object> { TestDataCreator.USER_USERNAME => 'SubmitterUser1@testorg.com', TestDataCreator.USER_LOCALE => 'en_US', TestDataCreator.PROFILE_NAME => 'GPM_Standard_User', TestDataCreator.PERMISSION_SET_NAME => 'GPM_FOUNDATION' }, new Map<String, Object> { TestDataCreator.USER_USERNAME => 'AdminUser1@testorg.com', TestDataCreator.USER_LOCALE => 'en_US', TestDataCreator.PROFILE_NAME => 'System Administrator', TestDataCreator.PERMISSION_SET_NAME => 'GPM_ADMIN' } } ); localUser1 = TestDataCreator.getUser('LocalUser1@testorg.com'); regionUser1 = TestDataCreator.getUser('RegionUser1@testorg.com'); globalUser1 = TestDataCreator.getUser('GlobalUser1@testorg.com'); submitterUser1 = TestDataCreator.getUser('SubmitterUser1@testorg.com'); adminUser1 = TestDataCreator.getUser('AdminUser1@testorg.com'); // Configure Data system.runAs(globalUser1) { List<MnPrice__c> prices = TestDataCreator.createPrices( new List<Map<String, Object>> { new Map<String, Object> { TestDataCreator.COUNTRY_NAME => 'TestCountry', TestDataCreator.COUNTRY_ISO_CODE => 'C1', TestDataCreator.CURRENCY_ISO_CODE => 'USD', TestDataCreator.COUNTRY_MULTICURRENCY_ENABLED => true, TestDataCreator.REGION_NAME => 'TestRegion', TestDataCreator.PRODUCT_SKU_NAME => 'TestProduct1', TestDataCreator.PRODUCT_NAME => 'TestFamily', TestDataCreator.PRICE_TYPE_NAME => 'Ex-Manufacturer', TestDataCreator.COUNTRY_PRODUCT_SKU_CURRENT_STATUS => 'Approved But Not Marketed', TestDataCreator.PRICE => 10, TestDataCreator.PRICE_START_DATE => system.today().addMonths(-1), TestDataCreator.PRICE_END_DATE => null }, new Map<String, Object> { TestDataCreator.COUNTRY_NAME => 'TestCountry', TestDataCreator.COUNTRY_ISO_CODE => 'C1', TestDataCreator.CURRENCY_ISO_CODE => 'USD', TestDataCreator.COUNTRY_MULTICURRENCY_ENABLED => true, TestDataCreator.REGION_NAME => 'TestRegion', TestDataCreator.PRODUCT_SKU_NAME => 'TestProduct2', TestDataCreator.PRODUCT_NAME => 'TestFamily', TestDataCreator.PRICE_TYPE_NAME => 'Ex-Manufacturer', TestDataCreator.COUNTRY_PRODUCT_SKU_CURRENT_STATUS => 'Approved But Not Marketed', TestDataCreator.PRICE => 200, TestDataCreator.PRICE_START_DATE => system.today().addMonths(-1), TestDataCreator.PRICE_END_DATE => null }, new Map<String, Object> { TestDataCreator.COUNTRY_NAME => 'TestCountry', TestDataCreator.COUNTRY_ISO_CODE => 'C1', TestDataCreator.CURRENCY_ISO_CODE => 'USD', TestDataCreator.COUNTRY_MULTICURRENCY_ENABLED => true, TestDataCreator.REGION_NAME => 'TestRegion', TestDataCreator.PRODUCT_SKU_NAME => 'TestProduct3', TestDataCreator.PRODUCT_NAME => 'TestFamily2', TestDataCreator.PRICE_TYPE_NAME => 'Ex-Manufacturer', TestDataCreator.COUNTRY_PRODUCT_SKU_CURRENT_STATUS => 'Actively Marketed', TestDataCreator.PRICE => 200, TestDataCreator.PRICE_START_DATE => system.today().addMonths(-1), TestDataCreator.PRICE_END_DATE => null } } ); country = TestDataCreator.getCountry('TestCountry'); family = TestDataCreator.getProduct('TestFamily'); family2 = TestDataCreator.getProduct('TestFamily2'); priceLevel = TestDataCreator.getCountryPriceType('TestCountry','Ex-Manufacturer'); product1 = TestDataCreator.getProductSKU('TestProduct1'); product2 = TestDataCreator.getProductSKU('TestProduct2'); countryProductSKU1 = TestDataCreator.getCountryProductSKU('TestCountry','TestProduct1'); countryProductSKU2 = TestDataCreator.getCountryProductSKU('TestCountry','TestProduct2'); productPricing1 = prices[0]; productPricing2 = prices[1]; } // Configure Data Visibility system.runAs(globalUser1) { GPUserManager.setAsynchronous(false); // local and region users have access to all products MnUserConfig__c submitterUserConfig = new MnUserConfig__c( User__c=submitterUser1.Id, ProductAccess__c=GPUserManager.PICKLIST_OPTION_ALL_PRODUCTS ); MnUserConfig__c localUserConfig = new MnUserConfig__c( User__c=localUser1.Id, ProductAccess__c=GPUserManager.PICKLIST_OPTION_ALL_PRODUCTS ); MnUserConfig__c regionUserConfig = new MnUserConfig__c( User__c=regionUser1.Id, ProductAccess__c=GPUserManager.PICKLIST_OPTION_ALL_PRODUCTS ); MnUserConfig__c globalUserConfig = new MnUserConfig__c( User__c=globalUser1.Id, ProductAccess__c=GPUserManager.PICKLIST_OPTION_ALL_PRODUCTS, RegionAccess__c=GPUserManager.PICKLIST_OPTION_EDIT, PriceAccess__c=GPUserManager.PICKLIST_OPTION_UNPUBLISHED ); insert new List<MnUserConfig__c>{submitterUserConfig,localUserConfig,regionUserConfig,globalUserConfig}; // local and submitter users have edit access to country MnUserCountry__c submitterUserCountry = new MnUserCountry__c( MnUserConfig__c=submitterUserConfig.Id, MnCountry__c=country.Id, Access__c=GPUserManager.PICKLIST_OPTION_EDIT, PriceAccess__c=GPUserManager.PICKLIST_OPTION_UNPUBLISHED ); MnUserCountry__c localUserCountry = new MnUserCountry__c( MnUserConfig__c=localUserConfig.Id, MnCountry__c=country.Id, Access__c=GPUserManager.PICKLIST_OPTION_EDIT, PriceAccess__c=GPUserManager.PICKLIST_OPTION_UNPUBLISHED ); insert new List<MnUserCountry__c>{submitterUserCountry,localUserCountry}; // region user has edit access to region MnUserRegion__c regionUserRegion = new MnUserRegion__c( MnUserConfig__c=regionUserConfig.Id, MnRegion__c=TestDataCreator.getRegion('TestRegion').Id, Access__c=GPUserManager.PICKLIST_OPTION_EDIT, PriceAccess__c=GPUserManager.PICKLIST_OPTION_UNPUBLISHED ); // local user has read access to TestRegion MnUserRegion__c localUserRegion = new MnUserRegion__c( MnUserConfig__c=localUserConfig.Id, MnRegion__c=TestDataCreator.getRegion('TestRegion').Id, Access__c=GPUserManager.PICKLIST_OPTION_READ, PriceAccess__c=GPUserManager.PICKLIST_OPTION_UNPUBLISHED ); insert new List<MnUserRegion__c> {regionUserRegion, localUserRegion}; } // Configure Approvers system.runAs(globalUser1) { MnApprover__c approver1 = new MnApprover__c( MnApprover__c=localUser1.Id, MnCountry__c=country.Id, MnProduct__c=family.Id, Level__c=1 ); MnApprover__c approver2 = new MnApprover__c( MnApprover__c=regionUser1.Id, MnCountry__c=country.Id, MnProduct__c=family.Id, Level__c=2 ); MnApprover__c approver3 = new MnApprover__c( MnApprover__c=globalUser1.Id, MnCountry__c=country.Id, MnProduct__c=family.Id, Level__c=3 ); insert new List<MnApprover__c>{approver1,approver2,approver3}; } } }
TestDataCreator类是自己创建的类
public class TestDataCreator { public static final String PROFILE_NAME = 'profileName'; public static final String PERMISSION_SET_NAME = 'permissionSetName'; public static final String USER_ALIAS = 'userAlias'; public static final String USER_EMAIL = 'userEmail'; public static final String USER_EMAIL_ENCODING = 'userEmailEncoding'; public static final String USER_LAST_NAME = 'userLastName'; public static final String USER_FIRST_NAME = 'userFirstName'; public static final String USER_USERNAME = 'userUsername'; public static final String USER_LOCALE = 'userLocale';
system.runAs(globalUser1)方法的作用:Changes the current user to the specified user.
注:测试数据不会添加到数据库中,因此不会存在存留问题,不需要delete操作。这个测试数据具体是怎么做的有时间可以研究下。
3.定义测试方法
static testMethod void testCreateNewPriceApproval() { TestData td = new TestData(); MnPriceApprovalPrice__c price = new MnPriceApprovalPrice__c( MnCountry__c=td.country.Id, MnProduct__c=td.family.Id, MnCountryProductSKU__c=td.countryProductSKU1.Id, MnCountryPriceType__c=td.priceLevel.Id, ProposedPrice__c=9, StartDate__c=DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0)).addDays(2) ); Test.startTest(); User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ]; System.runAs ( thisUser ){ MnCustomSetting__c cs = new MnCustomSetting__c( Name='PriceRequest_SKUTableColumns', Value__c='MnCountryPriceType__c.Published__c,read,MnCountryPriceType__c.Name,read, MnPriceApprovalPrice__c.FinalPercent__c,edit, MnPriceApprovalPrice__c.CurrencyIsoCode,edit'); insert cs; } System.runAs(td.submitterUser1) { PageReference paPage = Page.PriceApproval; paPage.getParameters().put(MnPageParameters.PRODUCT_ID_PARAM, td.family.Id); paPage.getParameters().put(MnPageParameters.COUNTRY_ID_PARAM, td.country.Id); Test.setCurrentPage(paPage); MnPriceApproval__c newPriceApproval = new MnPriceApproval__c( MnProduct__c=td.family.Id, MnCountry__c=td.country.Id ); // test constructor MnPriceApprovalFormController extension = new MnPriceApprovalFormController(); extension.setPriceApproval(newPriceApproval); System.assertEquals(true, extension.isExistExtraColumn()); System.assertEquals(4, extension.getSKUTableCustomCells(td.priceLevel, td.countryProductSKU1, price).size()); System.assertEquals(td.family.Id,extension.product.Id); System.assertEquals(td.country.Id,extension.country.Id); System.assertEquals(MnPriceApprovalFormController.SUBMIT_CONFIRMATION_MESSAGE,extension.getSubmitConfirmationMessage()); System.assertEquals(MnPriceApprovalFormController.DELETE_CONFIRMATION_MESSAGE,extension.getDeleteConfirmationMessage()); // check prices System.assertEquals(2,extension.priceApprovalPrices.size()); System.assertEquals(2,extension.productSKURows.size()); MnPriceApprovalFormController.ProductSKURow productSKURow = extension.productSKURows[0]; System.assertEquals(td.countryProductSKU1.Id,productSKURow.countryProductSKU.Id); System.assertEquals(1,productSKURow.priceTypeRows.size()); MnPriceApprovalFormController.PriceTypeRow priceTypeRow = productSKURow.priceTypeRows[0]; System.assertEquals(td.productPricing1.Price__c,priceTypeRow.currentPrice.Price__c); // save extension.getPriceApproval().ExpectingResponseDate__c = Date.today().addDays(1); priceTypeRow.approvalPrice.ProposedPrice__c = 9; priceTypeRow.approvalPrice.StartDate__c = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0)).addDays(2); extension.onSave(); MnPriceApproval__c priceApproval = MnPriceApprovalService.getApproval(extension.getPriceApproval().Id); System.assertEquals(9,priceApproval.MnPriceApprovalPrices__r[0].ProposedPrice__c); extension.setPriceApproval(priceApproval); System.assertEquals(true,extension.getIsSubmitter()); System.assertEquals(false,extension.getIsApprover()); System.assertEquals(true,extension.getSaveButtonRendered()); System.assertEquals(true,extension.getSubmitButtonRendered()); System.assertEquals(false,extension.getRecallButtonRendered()); System.assertEquals(false,extension.getFinalizeButtonRendered()); System.assertEquals(false,extension.getApproveRejectButtonRendered()); System.assertEquals(false,extension.getSubmittedDateRendered()); System.assertEquals(false,extension.getRejectSKURendered()); // delete extension.onDelete(); List<MnPriceApproval__c> priceApprovals = [SELECT Id FROM MnPriceApproval__c]; System.assertEquals(0,priceApprovals.size()); // start over and submit newPriceApproval = new MnPriceApproval__c( MnProduct__c=td.family.Id, MnCountry__c=td.country.Id ); extension = new MnPriceApprovalFormController(); extension.setPriceApproval(newPriceApproval); productSKURow = extension.productSKURows[0]; priceTypeRow = productSKURow.priceTypeRows[0]; extension.getPriceApproval().PriceChangeType__c='type1'; extension.getPriceApproval().PriceChangeReason__c='reason1'; extension.getPriceApproval().PriceChangeFlexibility__c='flex1'; extension.getPriceApproval().ExpectingResponseDate__c = Date.today().addDays(1); productSKURow.countryProductSKU.CurrentStatus__c = 'Actively Marketed'; priceTypeRow.approvalPrice.ProposedPrice__c = 9; priceTypeRow.startDate.ExpectingResponseDate__c = Date.today().addDays(2); extension.onSave(); extension.onSubmit(); priceApproval = MnPriceApprovalService.getApproval(extension.getPriceApproval().Id); System.assertEquals(9,priceApproval.MnPriceApprovalPrices__r[0].ProposedPrice__c); System.assertEquals('Actively Marketed',priceApproval.MnPriceApprovalPrices__r[0].CurrentStatus__c); System.assertEquals(MnPriceApprovalService.APPROVAL_STATUS_PENDING,priceApproval.ApprovalStatus__c); // then recall // TODO: // PageReference paPage = Page.PriceApproval; // paPage.getParameters().put('Id', priceApproval.Id); // Test.setCurrentPage(paPage); // ApexPages.StandardController controller = new ApexPages.StandardController(new MnPriceApproval__c()); // MnPriceApprovalFormController extension = new MnPriceApprovalFormController(controller); // exension.onRecall(); // priceApproval = MnPriceApprovalService.getApproval(extension.priceApproval.Id); // System.assertEquals(MnPriceApprovalService.APPROVAL_STATUS_RECALLED,priceApproval.ApprovalStatus__c); } Test.stopTest(); }
注:Test.startTest(),表示开始测试;Test.stopTest(),表示结束测试。
System.assertEquals(true, extension.isExistExtraColumn())为断言,这样就相当于测试了类中的方法或变量。
测试覆盖率需要达到75%。
每个类都应该拥有相应的测试类。
测试的难点就在于:测试数据的构造,以及给相应的方法传递怎样的数据。