如何提高Apex中这个测试类的覆盖率?

时间:2018-04-02 18:40:51

标签: salesforce apex-code apex

我创建了一个测试类,其代码覆盖率为51%,直到第34行。

此外,我试图满足条件,但我不能。现在我不知道如何处理100%的代码覆盖率。

这是Apex课程:

public class AssignProjectController {
public String CaseIds;
public String status {get;set;}
public List<Project__c> activeProjects {get;set;}
public String keyWordSearched {get;set;}
public Id projectId {get;set;}
public AssignProjectController (){       
    CaseIds = ApexPages.currentPage().getParameters().get('id');             
}

public void getProjects(){
 status  = '';
  String searchQuery = 'FIND \'' + keyWordSearched + '*\' IN ALL FIELDS RETURNING Project__c (id,Name,Description__c where Status__c =\'Active\')';
  try{
  List<List<Project__c >> searchList = search.query(searchQuery); 
  activeProjects  = searchList[0];
  if(activeProjects.size() == 0) status = 'No search result found.';
  }catch(Exception ex){
    system.debug('ex..'+ex.getMessage());
    }}}
public PageReference assignProjectToCases(){ 
    List<Case__c> customSettingList = Case__c.getall().values();
    List<String> settingRecordTypeList = new List<String>();         
    for(Case__c caseObj:customSettingList){
        settingRecordTypeList.add(caseObj.Name);
    }   
   List<RecordType> recordTypeListData = [SELECT Id FROM RecordType WHERE SObjectType = 'Case' and Name In : settingRecordTypeList];        
    if(CaseIds != null){
        List<String> caseIDList = new List<String>(); 
        caseIDList = CaseIds.split(',');                        
        if([Select id from Case where Id In : caseIDList and RecordType.Id NOT In : recordTypeListData].size() > 0){
           status  = 'failed';
        }else{
             List<Case> cases = [Select id,Project__c,RecordType.Name from Case where Id In : caseIDList and RecordType.Id In : recordTypeListData];
             if(cases.size() > 0){
                for(case caseOb: cases){            
                    caseOb.Project__c = projectId ;
                }        
                try{
                    update cases ;
                    status  = 'Changes are scheduled';
                 }catch(Exception ex){
                    system.debug('AssignProjectController :::'+ex.getMessage());
                    status  = 'Something Went Wrong';
                }}}}
     return null;
 }}

这是我试图解决的测试类

 @isTest public class TestAssignProjectController {
public static Project__c insertProject(){
    Project__c proObj = new Project__c();
    proObj.Name = 'testProject';
    proObj.Status__c = 'Active';
    proObj.Description__c = 'for testing';
    proObj.Internal_Email_Alias__c = 'a@test.com';
    return proObj;
}
public static Account getAccount(){
    Account accoObj = new Account();
    accoObj.Name  = 'testAcc';
    accoObj.Location__c = 'testLocation';
    accoObj.Type = 'CM';
    accoObj.BillingCountry = 'United States';
    return accoObj;
}
public static Contact insertContact(Account accObj){
    Contact conObj = new Contact();
    conObj.FirstName = 'test';
    conObj.LastName = 'testLastname';
    conObj.AccountId = accObj.Id;
    conObj.Email = 'abc@gmail.com';
    return conObj;
}
 public static Id getTechTypeId(){
    return Schema.SObjectType.Case.getRecordTypeInfosByName().get('Tech ').getRecordTypeId();
}
public static Case insertCase(String conId, String proId){
    Case caseObj = new Case();
    caseObj.Project__c = proId;
    caseObj.ContactId = conId;
    caseObj.Status = 'Open';
    caseObj.Inquiry_Type__c = 'All'; 
    caseObj.Subject = 'TestSubject';
    caseObj.Description = 'TestDescription';
    caseObj.Case_Is_Reopened__c = false;
    caseObj.RecordTypeId = getTechTypeId();
    return caseObj;
}
public static testmethod void testMethodExecution(){
    AssignController asigncon = new AssignController ();

    Project__c proObj = insertProject();
    insert proObj;
    System.assertEquals(proObj.Status__c,'Active');
    Account accObj = getAccount();
   insert accObj;
   System.assertNotEquals(accObj.Id,null);
    Contact conObj = insertContact(accObj);
   insert conObj;
   System.assertNotEquals(conObj.Id,null);
   Case caseObj = insertCase(conObj.Id, proObj.Id);
   insert caseObj; 
   system.debug(caseObj);  
  //Set baseURL & case ID
    PageReference pageRef = Page.Assign;
    pageRef.getParameters().put('id',caseObj.id+',');
    AssignController asigncon1 = new AssignController ();
    asigncon1.getProjects();
    asigncon1.assignProjectToCases();
}}

1 个答案:

答案 0 :(得分:1)

如果您在引用<ion-row col-12> <ion-col col-4 [dragula]='"module-layout"' class="module-container" id ="firstBag"></ion-col> <ion-col col-4 [dragula]='"module-layout"' class="module-container" id ="secondBag"></ion-col> </ion-row> 此声明,那么肯定存在插入案例的问题。确保if(cases.size() > 0)正在运行并在Salesforce后端插入数据。

如果case对象中没有数据,则测试方法不能覆盖if语句。