+ Start a Discussion
ChinnoduChinnodu 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body] in test class

Hi Experts,

I have written a test class , test method does not passed and i got cobut i am getting  below error. how to i will fix , kindly suggest me.

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body].


public class caseattachment
{
public case objcase{get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;} 
public Blob fileBody{get;set;}

    public caseattachment(Apexpages.standardcontroller controller)
    {
        objcase = new case();
        myAttachment =new Attachment();
    }
    public pagereference save()
    {
        insert objcase;
        System.debug('@@@@@fileBody'+fileBody);     
        myAttachment  = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody; 
              myAttachment.Name = 'Logo_'+objcase.id+'.jpeg' ; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }
}

Please see attached the my screen shot User-added image
@isTest
private class caseattachment1{

    static testMethod void testCaseExt() {
        // Create the Case Record.
        Case cas = new Case( Priority = 'Medium',Status= 'Working',Origin= 'Phone'); 
        insert cas;
        Attachment attach=new Attachment();     
        attach.Name='Unit Test Attachment';
        Blob fileBody=Blob.valueOf('Unit Test Attachment Body');
        attach.body=fileBody;
        attach.parentId=cas.id;
        insert attach;
        
        
         ApexPages.StandardController sc = new ApexPages.StandardController(cas);
        caseattachment cec= new caseattachment(sc);
        
         
        cec.save();
       
    }
}

 
Nayana KNayana K
@isTest
private class caseattachment1{

    static testMethod void testCaseExt() {
        Case cas= new Case();
        
         ApexPages.StandardController sc = new ApexPages.StandardController(cas);
        caseattachment cec= new caseattachment(sc);
        // Create the Case Record.
        cec.objcase = new Case( Priority = 'Medium',Status= 'Working',Origin= 'Phone'); 
        
        
        Blob fileBody=Blob.valueOf('Unit Test Attachment Body');
        cec.fileBody=fileBody;
        // call save and verify result 
        cec.save();
      // verify if case is inserted 
       System.assertNotEquals(null, cec.objcase.Id);
    }
}

Please try this once