function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NavneethNavneeth 

Need to write a test class for the below mentioned class

Hi,

 i need to write test class for the below mentioned class. This class displays the case milestone resolution target time. 
Pls help me write test class for this. 


Class code:- 

public class Clock {
Public String  casename {get;set;}
    public String getCaseMilestone() {
        return null;
    }


    public Clock() {

    }


List<CaseMilestone> sr = new List<CaseMilestone>();
List<CaseMilestone> srresponse = new List<CaseMilestone>();
ID myId;
public Datetime targetDate{get;set;}
public Datetime targetResponseDate{get;set;}

public Clock(ApexPages.StandardController controller) {
   myId = ApexPages.CurrentPage().getParameters().get('id');
  
   sr = [select TargetDate from CaseMilestone where CaseId =: myId and MilestoneTypeId ='557900000008Orj'];
srresponse = [Select TargetDate from CaseMilestone where CaseId =: myId and MilestoneTypeId ='557900000008OrU'];
  
   for(CaseMilestone s1 : sr)
   {
       targetDate  = s1.TargetDate;
    
   }
  
   for(CaseMilestone s2 : srresponse)
   {
       targetResponseDate  = s2.TargetDate;  
    }
  }
}
Sonam_SFDCSonam_SFDC
Hi Navneeth,

Would help if you could share the test  code you've written so far so the community can help you tweak to correct the code.

Please go through the following sample test codes:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
NavneethNavneeth
Hi Sonam , following is my test code

  @isTest
    private class testClock {
    static testMethod void testClock () {
    List<CaseMilestone> sr = new List<CaseMilestone>();
List<CaseMilestone> srresponse = new List<CaseMilestone>();
       CaseMilestone w = new CaseMilestone();
       Case m = new case(Subject='Power output less than 80%');
       update m;
      
        sr = [select TargetDate from CaseMilestone where CaseId = '5009000000NHABI' and MilestoneTypeId ='557900000008Orj'];
srresponse = [Select TargetDate from CaseMilestone where CaseId = '5009000000NHABI' and MilestoneTypeId ='557900000008OrU']; 
    }
    
}

This is saving but its showing 0% Code Coverage. Am i missing something. Also in the testcode if i try to add test data then i get an error "Field not writable". I guess CaseMilestone field objects are not writable. how to overcome this ? pls help