Lavi__c
Lavi__c

Reputation: 57

How to improve the coverage of this test class in Apex?

I have created a test class with 51% code coverage till line no 34.

Further, I tried to satisfy if condition but I couldn't. Now I am not getting how to do with 100% code coverage.

Here is the Apex class:

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;
 }}

Here is the test class- which I tried to resolve

 @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 = '[email protected]';
    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 = '[email protected]';
    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();
}}

Upvotes: 0

Views: 4364

Answers (1)

Noor A Shuvo
Noor A Shuvo

Reputation: 2807

If you are referring if(cases.size() > 0) this statement, then surely there is problem of inserting the case. Make sure that insert caseObj; is working and inserts data in Salesforce backend.

If there is no data in case object, the test method cannot cover the if statement.

Upvotes: 1

Related Questions