+ Start a Discussion
dbimndbimn 

Not getting complete code coverage from this test, any thoughts?

Hi All,

I'm not getting code coverage I need on this class.  Red lines aren't covered.  The test class is at the end of this post..

Any thoughts on how to cover these lines?  Thanks for any help!

 

UpdateMaxReviewDate (Code Covered: 69%)

 

 line source
 1  public with sharing class UpdateMaxReviewDate{
 2  
 3   public void updReviews(){
 4  
 5   Map<Id, Date> MaxDates = new Map<Id, Date>();
 6   List<IMNAccount__c> lstToUpdate = new List<IMNAccount__c>();
 7  
 8   for (AggregateResult ard : [Select imnaccount__c,
 9   Max(Review_Date__c) rd From Report_Review__c
 10   GROUP BY imnaccount__c]) {
 11  
 12   MaxDates.put(String.valueOf(ard.get('imnaccount__c')),(Date) ard.get('rd'));
 13  
 14  }
 15   Set <Id> imnacctSet = new Set<Id>();
 16   imnacctSet = MaxDates.keySet();
 17   for (IMNAccount__c imn : [Select i.Last_Report_Review__c, i.Id,
 18   i.Company__r.PM_Report_Review__c, i.PM_Report_Review_Date__c
 19   From IMNAccount__c i
 20   WHERE i.Id IN : imnacctSet]){
 21   imn.Last_Report_Review__c = MaxDates.get(imn.Id);
 22   imn.PM_Report_Review_Date__c = imn.Company__r.PM_Report_Review__c;
 23   lstToUpdate.add(imn);
 24   }
 25   if(lstToUpdate.size() > 0)
 26   update lstToUpdate;
 27  }

 

Here's my Test Code

 

@isTest
private class UpdateMaxReviewDateTest {

public static testMethod void testReviewDates() {
        
        Date d = Date.today();
        
        //Create Test Account
        Account atest = new Account();
        atest.Name = 'New Test Account';
        atest.PM_Report_Review__c = d;
        Database.insert(atest);
        
        //Create Test imnaccount
        IMNAccount__c itest = new IMNAccount__c();
        itest.Name = 'New Test Imnaccount';
        Database.insert (itest);
        
        //Create Test Report Review
        List<Report_Review__c> rList = new List<Report_Review__c>();
        for (Integer i=0; i<21; i++){
            Report_Review__c rtest = new Report_Review__c();
            rtest.Review_Date__c = d-i;
            rList.add(rtest);
        }
        
        Database.insert(rList);    
        
        Test.startTest();
        
        //instantiate class to be tested and call method
        UpdateMaxReviewDate urd = new UpdateMaxReviewDate();
        urd.updReviews();
        
        
        //validate
        for (IMNAccount__c imn : [Select i.Last_Report_Review__c, i.Id,
                            i.Company__r.PM_Report_Review__c, i.PM_Report_Review_Date__c
                            From IMNAccount__c i]){
                            Date a = imn.Company__r.PM_Report_Review__c;
                            Date b = imn.PM_Report_Review_Date__c;
                            System.assertEquals(a,b);
                                
                            AggregateResult agr = [Select Max(Review_Date__c) rd From Report_Review__c
                                        WHERE imnaccount__c = :imn.Id];
                            Date c = (Date) agr.get('rd');
                            Date e = imn.Last_Report_Review__c;
                            System.assertEquals(c,e);   
           }
           Test.stopTest();
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Also,

 


Please make the change  in your test class from

 

@isTest

 

to

 

@isTest(SeeAllData-true)

All Answers

steve456steve456

Based on your test class ,please do not give SOQL queries instead create test data .

 

 

PM_Report_Review_Date__c= System.Today()  ----if its a Date field

 

 

PM_Report_Review_Date__c=System.Now()------if its a Date/Time field 

 

 

 

and then do the update .You should be through

 

 

 

Vinit_KumarVinit_Kumar

Also,

 


Please make the change  in your test class from

 

@isTest

 

to

 

@isTest(SeeAllData-true)

This was selected as the best answer
dbimndbimn

thanks for replies.  i"ll let you know when I've sorted it out.

dbimndbimn

Thank you, steve

 

I'm a little confused by your reply.  I did create test data with the test class.  Can you elaborate a little more on removing the soql queries and just adding the line you suggested, PM_Report_Review_Date__c= System.Today().

 

Are you saying just include this in the test data creation?

How does this validate?  Can you show me example code of what you would modify?

 

Really appreciate it.

dbimndbimn

Adding @isTest(SeeAllData=true) gave me 100% coverage, but I don't understand why it made the difference.  I understand it gives the test class access to all records, not just test records, but why did this make the difference in test coverage?

Vinit_Kumar, can you explain?

 

Thank you

dbimndbimn

Never mind.  I see now that I forgot to establish relationships with my test data.  So nothing was being tested until I used the See all data code.  Fixed all.  Again, many thanks for the advice.

Vinit_KumarVinit_Kumar

Hi,

 

After API version 24.0 salesforce has applied this annotation,if you want to access data which are available inside your org,you need to use (SeeAllData=true),by default it is false.Please go through the below link to know more :-

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm