0

In the following test class I am getting only 20% code coverage, and I am able to track it up to some level. Actually I am getting 100% code coverage before adding the following AND condition to the soql query "AND Id NOT IN (SELECT ParentId FROM CaseComment WHERE CreatedDate = LAST_N_DAYS:3)" . But after adding it to my main query I am getting only 20%, any test data need to be added here?.

APEX CLASS:

public class UntouchedReportController { 

public list<WrapperData> getReport(){ 

Map<String,WrapperData> LatesMap = new Map<String,WrapperData>(); 

for(Case c:[Select Id,Main_issue__c,SubIssue__c, CaseNumber, status, 
   Owner.Name, (Select Id,CreatedBy.name, CreatedDate,LastModifiedDate 
   From CaseComments ORDER BY CreatedDate DESC LIMIT 1) From Case WHERE 
   Status IN('Processing','New Seller Message','Pending Closure') AND 
   Id NOT IN (SELECT ParentId FROM CaseComment WHERE CreatedDate = 
   LAST_N_DAYS:3)ORDER BY Owner.Name]){ 

    if(!LatestMap.Containskey(c.CaseNumber)){ 
      for(CaseComment comm : c.CaseComments) { 
      WrapperData wrap = new WrapperData(); 

      wrap.CaseOwner = c.Owner.name; 
      wrap.CaseNumber = c.CaseNumber; 
      wrap.subject = c.subject; 
      wrap.Mainissue =c.Main_Issue__c; 
      wrap.Subissue =c.Subissue__c; 
      wrap.Status = c.Status; 
      wrap.CommentBody = comm.CommentBody; 
      wrap.LastModifiedBy = comm.CreatedBy.name; 
      wrap.LastModifiedDate = comm.CreatedDate; 

      LatestMap.put(c.CaseNumber,wrap); 
     }
   } 
 } 
  return LatesMap.values(); 
} 

// Inner class 
public class WrapperData{ 

  public string CaseOwner{get;set;} 
  public string CaseNumber{get;set;} 
  public string CommentBody{get;set;} 
  public string Mainissue{get;set;} 
  public string Subissue{get;set;} 
  public string Status{get;set;} 
  public string LastModifiedBy{get;set;} 
  public datetime LastModifiedDate{get;set;} 
  } 
}

TEST CLASS:

  @isTest 
    public class UntouchedReportControllerTest{ 

    @TestSetup Public static void testSetUp(){ 

    List<Case> cslist = new List<Case>(); 
    Case testCs = new Case(Main_Issue__c = 'Login', 
    Subissue__c = 'Password', Status = 'Processing', Subject = 'Test'); 

   cslist.add(testCs); 

   Case testCs1 = new Case(Main_Issue__c = 'Site', Subissue__c = 'Items', 
   Status = 'Pending Closure', Subject = 'Test1'); cslist.add(testCs1 ); 

   insert cslist; 

   list<CaseComment> cclist = new list<CaseComment>(); 

    for(integer i=0; i< cslist.size();i++){ 
       cclist.add(new CaseComment(ParentId=cslist[i].id,CommentBody='New 
       comment',CreatedDate=System.now() )); 
      }
       insert cclist;
     } 

   static testMethod void getReportTest(){ 

    list<Casecomment> commList = [select ParentId, CreatedDate, 
    LastModifiedDate, CreatedBy.name from Casecomment]; 

    UntouchedReportController untRep = new UntouchedReportController(); 
    untRep.getReport(); 
    system.assertEquals(commList.size(),2); 
    }
   }

1 Answer 1

1

Your test data does not match the query in your main code.

Id NOT IN (SELECT ParentId FROM CaseComment WHERE CreatedDate = LAST_N_DAYS:3)

means that you're finding cases with no comments in the last three days.

But you create CaseComment records in your unit test:

for(integer i=0; i< cslist.size();i++){ 
   cclist.add(new CaseComment(ParentId=cslist[i].id,CommentBody='New 
   comment',CreatedDate=System.now() )); 
  }
   insert cclist;

ensuring that your Case doesn't match the query, meaning that the list body never executes.

You can use the method setCreatedDate() from the Test class to change the audit field CreatedDate on your Case Comments. This will allow you to create test data that matches your code's query by setting the creation date of the Case Comments prior to 3 days ago.

2
  • that's some useful info, I tried in the following way. For case it's not showing any issue, for comment it's saying like assertion failed looks like it's setting the date to parent cases instead of case comment. How can we fix this, any idea? DateTime theDat = DateTime.now().addDays(-4); Casecomment cscomnt = new Casecomment(ParentId = c.Id,commentBody = 'New comment'); insert cscomnt; Test.setCreatedDate(cscomnt.ParentId, theDat); cscomnt = [SELECT CreatedDate FROM Casecomment]; System.assertEquals(theDat, cscomnt.CreatedDate
    – Laxman
    Commented Jan 6, 2019 at 18:55
  • When you do setCreatedDate(), give it the Id of the record whose created date you want to set. If you pass cscomnt.ParentId, it will set the created date of the parent Case.
    – David Reed
    Commented Jan 6, 2019 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .