2

Based on the requirement ,we have created a vf page ,apex and test class written.When i run the test class ,the method gets passed but it shows 0 code coverage. I shall appreciate your help.

VF Controller Code :

public with sharing class MassCaseClose {

      public Case objCase {get;set;} 
      private List<Case> lstCase; 
      public MassCaseClose() 
      { 
           objCase = new Case(); 
           lstCase = new List<Case>(); 
           String strRecordIds = ApexPages.currentPage().getParameters().get('ids');
            set<Id> setCaseIds = new set<Id>();
            for(String strid : strRecordIds.split(','))
            {
                setCaseIds.add(id.valueof(strid.trim()));
            }
            lstCase = [SELECT Id, Status FROM Case WHERE Id IN: setCaseIds];
     } 
     public PageReference save() 
   { 
      for(Case objCaseRecord: lstCase) 
    { 
      objCaseRecord.Status = objCase.Status; 
      objCaseRecord.Reason_For_Resolution__c = objCase.Reason_For_Resolution__c; 
      objCaseRecord.Closer_Comment__c=objCase.Closer_Comment__c;
      objCaseRecord.Do_Not_Send_Email_Case_Closure__c = objCase.Do_Not_Send_Email_Case_Closure__c; 
    } 
      update lstCase; 
      return new pagereference('/500'); 

   } 
     public PageReference cancel() 
    { 
      return new pagereference('/500'); 
  } 

}

Test Class :

@isTest
public class MassCaseCloseTest{
    public static TestMethod void TestMassCaseClose(){

       Account a = new Account();
        a.Name ='Testing Account';        
        insert a;

        Contact c = new Contact();
        c.FirstName ='Testing';
        c.LastName ='Contact';
        insert c;

        Case cas = new Case();
        cas.Status='Closed';
        cas.Reason_For_Resolution__c='Spam';
        cas.Closer_Comment__c='Testing for the Mass closer of Cases';
        insert cas;   

        Test.startTest();
        ApexPages.CurrentPage().getparameters().put('ids',cas.id);
        MassCaseClose objCase = new MassCaseClose ();
        /*objCase.Status = 'Closed';
        objCase.Closer_Comment__c='Testing for the Mass closer of Cases';*/
        objCase.save();
        Test.stopTest();
    }   
     public static TestMethod void TestMassCaseClose1(){

       Account a = new Account();
        a.Name ='Testing Account';        
        insert a;

        Contact c = new Contact();
        c.FirstName ='Testing ';
        c.LastName ='Contact';
        insert c;

        Case cas = new Case();
        cas.Status='Closed';
        cas.Reason_For_Resolution__c='Spam';
        cas.Closer_Comment__c='Testing for the Mass closer of Cases';
        insert cas;   

        Test.startTest();
        ApexPages.CurrentPage().getparameters().put('ids',cas.id);
        MassCaseClose objCase = new MassCaseClose ();
        /*objCase.Status = 'Closed';
        objCase.Closer_Comment__c='Testing for the Mass closer of Cases';*/
        objCase.Cancel();
        Test.stopTest();
    }   
   }
  • @Himanshu,@Poet:Thanks for your reply....i still get errors....where one method gets executed and the other as fail...the code coverage is showing as 95%...it shows the line which didn't get covered as "return new pagereference('/500');" – SalesforceCRM AccountCRM Nov 19 '15 at 11:47
  • Could you update your question, possibly showing your updated test class. You have two return new pagereference('/500');. Which one isn't being covered? – Dan Jones Nov 19 '15 at 13:21
0

This is because you are not creating any instance of your class inside your test class

@isTest
public class MassCaseCloseTest{
public static TestMethod void TestMassCaseClose(){

   Account a = new Account();
    a.Name ='Testing EDB Account';        
    insert a;

    Contact c = new Contact();
    c.FirstName ='Testing EDB';
    c.LastName ='Contact';
    insert c;

    Case cas = new Case();
    cas.Status='Closed';
    cas.Reason_For_Resolution__c='Spam';
    cas.Closer_Comment__c='Testing for the Mass closer of Cases';
    insert cas;   

    Case cas1 = new Case();
    cas1.Status='Closed';
    cas1.Reason_For_Resolution__c='Spam1';
    cas1.Closer_Comment__c='Testing for the Mass closer of Case1s';
    insert cas1; 

    Test.startTest();
    ApexPages.CurrentPage().getparameters().put('ids',cas.id + ',' + cas1);
    /**********Class intance created so that we can call constructor and ave method
    MassCaseClose objCase = new MassCaseClose();
    objCase.save();
    Test.stopTest();
}   

}
0

In addition to Himanshu's answer, I'd also point out I had a similar problem with this recently,

For me at least, when I was running a test in the Developer Console, tests were passing but they weren't showing any line coverage and displaying 0/# lines covered.

I fixed this by going to "Test" and ticking "Always Run Asynchronously".

enter image description here

I appreciate this answer might not be exactly what you're looking for, but it is something to look out for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.