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

Error in CaseMilestone Controller Test class


   I have developed an apex class which displays the TargetDate of CaseMilestone related with a particular case.
The following is my Apex class:-

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;
  Now i need to write a test class for this class.  I have written a test class but it is not working. 

 The following is my test class

private class CaseTest{
static testmethod void testLoadData(){
String caseJSON = '{"attributes":{"type":"CaseMilestone","url":"/services/data/v25.0/sobjects/CaseMilestone/55590000000CiTX"},"Id":"55590000000CiTX","MilestoneTypeId":"557900000008OrU","TargetDate":"5/30/2014 6:01 PM"}';
CaseMilestone c = (CaseMilestone) JSON.deserialize(caseJSON, CaseMilestone.class );
System.debug('Test case:' + c.TargetDate);
System.debug('Test caseId:' + c.Id);
System.debug('Test caseStatus:' + c.MilestoneTypeId);

CaseMilestone c1 = new CaseMilestone();
c1.Id = c.Id;
//c1.MilestoneTypeId = c.MilestoneTypeId;
//c1.TargetDate= c.TargetDate;
update c1;

//System.debug('Test caseStatus1:' + c1.status);

This class saves but it doesnt cover anything [0% Code coverage].

I  have commented two lines of code in my above test class. This is because if i remove those comments then i get an compile error which states the "Field: CaseMilestone.MileStonetypeId is not a Writable Field". Same is the error with TargetDate field. It shows the error "Field: CaseMilestone.TargetDate is not a Writable Field". 

Pls help me tweak the code properly so that i get atleast Some code coverage. Why is this test class not covering any code. Why am i getting the Field not Writable error when i remove the comments.

If possible pls help me with the new code. Its really urgent. I need to Migrate data from one instance to another and i am lagging because of this. 

Awaiting for reply. Thanks a ton.

Your class is a controller extension for a VF page, but the test class you wrote looks like it is written for a trigger.

With triggers, updating a record like you did in your test class will invoke the trigger and hence code coverage will be achieved.

However, will controller extensions, you should call the classes explicitly to invoke the calss code. These links should help you understand better:

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.