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
RishavRishav 

test classe for controller extension

Hii,
 I am Writing a test class for  cotroller but it is not getting  pass , it is always throwing error in the controller part as"DML Exception".
This is my extension class 
 
public CPCommAttachCls(ApexPages.StandardController stdController) {
        
        objId = ApexPages.currentPage().getParameters().get('CaseID');
        objCaseRec = [select CaseNumber, Subject from case where case.id =: objId];
        caseNumber = objCaseRec.CaseNumber;
        subject = objCaseRec.Subject;
       
       // initializing caseComment
       comment = new CaseComment();
       comment.parentid=objid;
        
      // initializing the variable for Attachment process
      CaseAttachment = new Attachment(); 
      CaseAttachment.parentId=objid; 
        
    }      // Controller scope finished

And this is my test class for passing the controller
@isTest(SeeAllData=true)
public class CPCommAttachTestCls {
    public static TestMethod void Testclass()
    {  
    //  CaseComment comment = new CaseComment();
      Case c = Cse();
      ApexPages.StandardController sc = new ApexPages.StandardController(c);
      CpCommAttachCls ContCls = new CpCommAttachCls(sc); // This line giving error
      
        
    }
    static Case cse(){
    Case c = new Case();
    c.subject = 'LogMeIn Pro2';
    c.Description='Test Description';
    c.General_App_Area__c='A';
    c.Priority='High';
    c.Status='New';
    insert c;
    return c;
}

}

I am using the standard controller case. But it is giving me error.
pconpcon
So the problem is you are fetching the wrong thing from your controller.  To work with the standard controller, you controller should read
 
objId = ApexPages.currentPage().getParameters().get('id');

This is because the record id when coming from the standard controller is 'id' not 'CaseId'