Salesforce 需要帮助使我的Apex代码覆盖率达到75%
我目前得到58%的代码覆盖率,我需要75%的覆盖率才能部署到生产环境中 下面是我的Apex测试类,最下面是我的Apex类Salesforce 需要帮助使我的Apex代码覆盖率达到75%,salesforce,Salesforce,我目前得到58%的代码覆盖率,我需要75%的覆盖率才能部署到生产环境中 下面是我的Apex测试类,最下面是我的Apex类 @isTest(SeeAllData=true) public class VaultVFTEST { /*This is a test class for the class VaultVF which is a Visusalforce controller for the page vault */ static testMethod void VaultVFTES
@isTest(SeeAllData=true)
public class VaultVFTEST {
/*This is a test class for the class VaultVF which is a Visusalforce controller for
the page vault */
static testMethod void VaultVFTEST() {
//Create opp, hardcode the Deal_Registration_Id__c field with 'x111111111' and insert opp
//Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());
//insert opp;
//Create a vault record that will be submitted
Vault__c vlt = new Vault__c(
Scenario__c = 'BIG-IP front ending Firewalls',
isValidDealReg__c = true,
Status__c = 'Application Received',
Load_Balancer_is_a_Firewall__c = 'Yes',
Providing_DDos_Connection__c = 'Firewalls',
Providing_DDos_protection_except_ASM__c = 'Yes',
No_Traffic_Traverse__c = 'Yes',
Firewall_Vendor_Name__c = 'Firewall Vendor Company',
First_Name__c = 'Test First Name',
Last_Name__c = 'Test Last Name',
Company_Name__c = 'Test Company Name',
Phone__c = '(206) 946-3126',
//Email__c = 'test@email.com',
Country__c = 'United States',
Additional_Info__c = 'Test Additional Info'
//Related_Opp__c = opp.Id
);
//Setup page for standard controller
ApexPages.StandardController sc = new ApexPages.StandardController(vlt);
Test.startTest();
VaultVF controller = new VaultVF(sc);
Test.stopTest();
//Assert has errors method returns true
system.assert(controller.getHasErrors() == false);
//Build the captcha value
controller.getChar1();
controller.getChar2();
controller.getChar3();
controller.getChar4();
controller.getChar5();
controller.getChar6();
//Set captcha input value
controller.captchaInput = '';
//Attempted to submit form
controller.save();
//Assert terms checkbox was not selected
system.assert(controller.errorMsgs[0].startsWith('You must'));
//Assert verification code was incorrect
system.assert(controller.errorMsgs[1].startsWith('Verification code'));
//'Check' the box, set captcha value and then save
controller.scenario1 = true;
controller.captchaInput = controller.correctAnswer();
controller.save();
}
}
下面是我的课程
public with sharing class VaultVF {
public Vault__c vault {get; set;}
public List<String> errorMsgs {get; set;}
public String saveResult {get; set;}
public String dealReg {get; set;}
public String oppId {get; set;}
public String email {get; set;}
public Boolean scenario1{get; set;}
public Boolean scenario2{get; set;}
public Boolean scenario3{get; set;}
// Generates country dropdown from country settings
public List<SelectOption> getCountriesSelectList() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('', '-- Select One --'));
// Find all the countries in the custom setting
Map<String, Country__c> countries = Country__c.getAll();
// Sort them by name
List<String> countryNames = new List<String>();
countryNames.addAll(countries.keySet());
countryNames.sort();
// Create the Select Options.
for (String countryName : countryNames) {
Country__c country = countries.get(countryName);
options.add(new SelectOption(country.Two_Letter_Code__c, country.Name));
}
return options;
}
//Constructor, set defaults
public VaultVF(ApexPages.StandardController controller) {
vault = (vault__c)controller.getRecord();
//Populate list of random characters for CAPTCHA verification
characters = new List<String>{'a','b','c','d','e','f','g','h',
'i','j','k','m','n','p','q','r','s','t','u','v','w',
'x','y','z','1','2','3','4','5','6','7','8','9'
};
errorMsgs = new List<String>();
}
//Determine if page has error so errors message can be displayed
public boolean getHasErrors(){
if(ApexPages.hasMessages() == true){
if(errorMsgs == null){
errorMsgs = new List<String>();
}
//Loop through errors and add to a list
for(ApexPages.Message m : ApexPages.getMessages()){
if(m.getSummary().startsWith('Uh oh.')){
errorMsgs.add(String.valueOf(m.getSummary()));
}
}
}
return ApexPages.hasMessages();
}
public void save(){
errorMsgs = new List<String>();
//Make sure captcha was correct
if(validateCAPTCHA() == false){
errorMsgs.add('Verification code was incorrect.');
captchaInput = '';
}
//Make sure the Scenario is selected
if(scenario1 == true){
vault.Scenario__c = 'BIG-IP as a Firewall';
}else if(scenario2 == true){
vault.Scenario__c = 'BIG-IP providing firewall service in conjunction with other vendors';
}else if(scenario3 == true){
vault.Scenario__c = 'BIG-IP front ending Firewalls';
if(vault.Load_Balancer_is_a_Firewall__c == null){
errorMsgs.add('Please specify if the load balancer is a firewall.');
}
if(vault.Providing_DDos_Connection__c == null){
errorMsgs.add('Please specify if this is providing DDos connection level protection for firewalls or both.');
}
if(vault.Providing_DDos_protection_except_ASM__c == null){
errorMsgs.add('Please specify if we are providing DDos protection (except ASM).');
}
if(vault.No_Traffic_Traverse__c == null){
errorMsgs.add('Please specify if there is Traffic that does not traverse the Firewalls.');
}
if(vault.Firewall_Vendor_Name__c == null){
errorMsgs.add('Please specify a Firewall Vendor.');
}
} else {
errorMsgs.add('Please select one of three Scenarios that is similar to your setup');
}
//-----Need to make sure deal registration number on opportuntiy is valid.-----
//Set some sort of is valid deal reg flag to false. We will assume the deal reg Id entered is invalid until we determine it is valid
vault.isValidDealReg__c = false;
//Query Account Id, and Deal_Registration_Id__c from the opportunity object where the deal registration Id equals the value entered in the form
List<Opportunity> opps = [select Id, Deal_Registration_ID__c from Opportunity where Deal_Registration_ID__c = :vault.Deal_Registration__c];
//check to see if registration id on the opp match the entered value
if(opps.size() > 0 && opps[0].Deal_Registration_ID__c == vault.Deal_Registration__c){
//If they do match set the valid deal reg Id flat to true
vault.isValidDealReg__c = true;
vault.Related_Opp__c = opps[0].Id;
//If they don't match then query all contacts on the account related to the opportunity
}
//If is valid deal reg flag is false add a message to the errorMSgs list indicated the entered
//deal registration is not valid with details on who they should contact
if(errorMsgs.size()>0){
//stop here
} else if(vault.isValidDealReg__c == false){
errorMsgs.add('Deal Registration # was incorrect. Please contact your representative.');
} else {
try{
vault.Status__c = 'Application Received';
insert vault;
saveResult = 'success';
}catch(exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Uh oh. Something didn\'t work quite right. Please contact partners@f5.com for assistance. \n\n' + e.getMessage()));
saveResult = 'fail';
}
}
}
//------------------------Code for CAPTCHA verification----------------------------
public String captchaInput {get; set;}
List<String> characters;
String char1;
String char3;
String char5;
//This methods simply returns a random number between 0 and the size of the character list
public Integer randomNumber(){
Integer random = Math.Round(Math.Random() * characters.Size());
if(random == characters.size()){
random--;
}
return random;
}
/*Here we have 6 get methods that return 6 random characters to the page.
For chars 1,3, and 5 (the black characters) we are saving the the values so
that we can compare them with the user's input */
public String getChar1(){
char1 = characters[randomNumber()];
return char1;
}
public String getChar2(){
return characters[randomNumber()];
}
public String getChar3(){
char3 = characters[randomNumber()];
return char3;
}
public String getChar4(){
return characters[randomNumber()];
}
public String getChar5(){
char5 = characters[randomNumber()];
return char5;
}
public String getChar6(){
return characters[randomNumber()];
}
public String correctAnswer(){
return char1 + char3 + char5;
}
public Boolean validateCAPTCHA(){
if(captchaInput.length() == 3 && captchaInput.subString(0,1) == char1 && captchaInput.subString(1,2) == char3 && captchaInput.subString(2,3) == char5){
return true;
}else{
return false;
}
}
}
public与共享类VaultVF{
公共保险库\uuu c保险库{get;set;}
公共列表错误msgs{get;set;}
公共字符串saveResult{get;set;}
公共字符串dealReg{get;set;}
公共字符串oppId{get;set;}
公共字符串电子邮件{get;set;}
公共布尔场景1{get;set;}
公共布尔场景2{get;set;}
公共布尔场景3{get;set;}
//从国家/地区设置生成国家/地区下拉列表
公共列表getCountriesSelectList(){
列表选项=新列表();
添加(新的SelectOption('''--selectone--');
//在自定义设置中查找所有国家/地区
Map countries=Country_uuc.getAll();
//按名字排序
List countryNames=新列表();
countryNames.addAll(countries.keySet());
countryNames.sort();
//创建选择选项。
for(字符串countryName:countryName){
Country\uu c Country=countries.get(countryName);
options.add(新的SelectOption(country.Two字母\代码\国家/地区,country.Name));
}
返回选项;
}
//构造函数,设置默认值
公共Vault vf(ApexPages.StandardController){
vault=(vault___c)控制器.getRecord();
//填充用于验证码验证的随机字符列表
字符=新列表{'a'、'b'、'c'、'd'、'e'、'f'、'g'、'h',
‘i’、‘j’、‘k’、‘m’、‘n’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’、‘w’,
‘x’、‘y’、‘z’、‘1’、‘2’、‘3’、‘4’、‘5’、‘6’、‘7’、‘8’、‘9’
};
errorMsgs=新列表();
}
//确定页面是否有错误,以便显示错误消息
公共布尔getHasErrors(){
if(ApexPages.hasMessages()==true){
if(errorMsgs==null){
errorMsgs=新列表();
}
//循环浏览错误并添加到列表中
for(ApexPages.Message m:ApexPages.getMessages()){
if(m.getSummary().startsWith('Uh-oh')){
errorMsgs.add(String.valueOf(m.getSummary());
}
}
}
返回ApexPages.hasMessages();
}
公共作废保存(){
errorMsgs=新列表();
//确保验证码是正确的
if(validateCAPTCHA()==false){
errorMsgs.add('验证代码不正确');
captchaInput='';
}
//确保选择了场景
如果(场景1==true){
vault.Scenario__c=‘作为防火墙的大IP’;
}else if(场景2==true){
vault.Scenario__c=‘与其他供应商一起提供防火墙服务的大IP’;
}否则如果(场景3==真){
vault.Scenario__c=‘大IP前端防火墙’;
如果(vault.Load\u Balancer\u是\u a\u Firewall\u c==null){
errorMsgs.add('请指定负载平衡器是否为防火墙');
}
if(提供DDos连接的保险库c==null){
errorMsgs.add('请指定这是为防火墙提供DDos连接级别保护还是同时提供这两种保护');
}
if(提供DDos保护的保险库,除ASM外c==null){
errorMsgs.add('请指定我们是否提供DDos保护(ASM除外)。';
}
如果(vault.No\u Traffic\u Travel\u c==null){
errorMsgs.add('请指定是否存在不通过防火墙的流量');
}
如果(vault.Firewall\u供应商名称\u\u c==null){
errorMsgs.add('请指定防火墙供应商');
}
}否则{
errorMsgs.add('请从三种与您的设置类似的方案中选择一种');
}
//-----需要确保opportuntiy上的交易注册号有效-----
//将某种类型的is valid deal reg标志设置为false。我们将假定输入的deal reg Id无效,直到我们确定它是有效的
vault.isValidDealReg\uu c=false;
//从opportunity对象中查询帐户Id和交易注册Id,其中交易注册Id等于表单中输入的值
List opps=[从Opportunity中选择Id、交易注册Id和c,其中交易注册Id和c=:vault.Deal注册c];
//检查opp上的注册id是否与输入的值匹配
如果(opps.size()>0&&opps[0].Deal\u Registration\u ID\uu c==vault.Deal\u Registration\uu c){
//如果他们匹配,则将有效的交易注册Id设置为true
vault.isValidDealReg_uuc=true;
vault.Related\u Opp\uuu c=opps[0].Id;
//如果不匹配,则查询与opportunity相关的帐户上的所有联系人
}
//如果有效的交易登记标志为false,则向errorMSgs列表中添加一条消息,指示输入的
//交易注册无效,其中包含应联系的联系人的详细信息
如果(errorMsgs.size()>0){
//停在这里
}else if(vault.isValidDealReg\uu c==false){
errorMsgs.add('交易登记不正确。请联系您的代表');
}否则{
试一试{
vault.Status__c='已收到申请';
插入保险库;
saveResult='success';
}捕获(例外e){
ApexPages.addMessage(新的ApexPages.Message(ApexPages.Severity.FATAL),“哦,有些东西不太正常。请联系partners@f5.com寻求帮助。\n\n'+e.getMessage());
saveResult='fail';
}
}
}
//------------------------验证码验证码----------------------------
公共字符串captchaInput{get;set;}
列出字符;
字符串char1;
字符串字符3;
字符串字符5;
//此方法仅返回一个介于0和字符列表大小之间的随机数
公共整数随机数(){
整数random=Math.Round(Math.random()*characters.Size());
if(random==characters.size()){
随机--;
}
返回随机;
}
/*这里我们有6个get方法,它们向页面返回6个随机字符。
对于chars 1、3和5(黑色
@isTest
private class VaultVFTEST {
/*This is a test class for the class VaultVF which is a Visusalforce controller for
the page vault */
static testMethod void VaultVFTEST() {
//Create a vault record that will be submitted
Vault__c vlt = new Vault__c(
Scenario__c = 'BIG-IP front ending Firewalls',
isValidDealReg__c = false,
Status__c = 'Application Received',
First_Name__c = 'Test First Name',
Last_Name__c = 'Test Last Name',
Company_Name__c = 'Test Company Name',
Phone__c = '(206) 946-3126',
Email__c = 'test@email.com',
Country__c = 'United States',
Additional_Info__c = 'Test Additional Info'
);
//Setup page for standard controller
ApexPages.StandardController sc = new ApexPages.StandardController(vlt);
Test.startTest();
VaultVF controller = new VaultVF(sc);
Test.stopTest();
//Assert has errors method returns true
system.assert(controller.getHasErrors() == false);
//Build the captcha value
controller.getChar1();
controller.getChar2();
controller.getChar3();
controller.getChar4();
controller.getChar5();
controller.getChar6();
//Set captcha input value
controller.captchaInput = '';
//Attempted to submit form
controller.save();
//Assert verification code was incorrect
system.assert(controller.errorMsgs[0].startsWith('Verification code'));
//'Check' the box, set captcha value and then save
controller.scenario3 = true;
controller.captchaInput = controller.correctAnswer();
controller.save();
Test.startTest();
insert vlt;
Test.stopTest();
}
}