+ Start a Discussion

test classe for controller extension

 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();
      // initializing the variable for Attachment process
      CaseAttachment = new Attachment(); 
    }      // Controller scope finished

And this is my test class for passing the controller
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';
    insert c;
    return c;


I am using the standard controller case. But it is giving me error.
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'