You need to sign in to do that
Don't have an account?
SFDC Guest
Test class for Attachment
Hi All,
Can you plz help me on covering the below two lines of attachment for below class. Current code coverage is 86%. Need to get 100%. Thanks
Test class:
@isTest
public class CaseCreationWithQAttachment_Test {
static testMethod void test1()
{
Case cs = new Case(Subject = 'Test', Origin = 'Phone', Status = 'New');
insert cs;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=cs.id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:cs.id];
System.assertEquals(1, attachments.size());
ApexPages.StandardController stdCtr = new ApexPages.StandardController(cs);
CaseCreationWithQAttachment ctr = new CaseCreationWithQAttachment(stdCtr);
ctr.caseData = cs;
ctr.attachData = attach;
ctr.customSave();
}
}
Can you plz help me on covering the below two lines of attachment for below class. Current code coverage is 86%. Need to get 100%. Thanks
Test class:
@isTest
public class CaseCreationWithQAttachment_Test {
static testMethod void test1()
{
Case cs = new Case(Subject = 'Test', Origin = 'Phone', Status = 'New');
insert cs;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=cs.id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:cs.id];
System.assertEquals(1, attachments.size());
ApexPages.StandardController stdCtr = new ApexPages.StandardController(cs);
CaseCreationWithQAttachment ctr = new CaseCreationWithQAttachment(stdCtr);
ctr.caseData = cs;
ctr.attachData = attach;
ctr.customSave();
}
}
Controller
Test Class
All Answers
Your main class does not have all the required fields that are required for inserting Attachment object.
As per this documentation (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_attachment.htm) you require three Fields:
I was able to increase your code coverage to 94% by using two different try catch blocks.
And this is the debug log that I am getting for line 20. This exception is most probably coming because you have not entered the other required fields for an attachment
PS : I have used your test class as it is and there is nothing wrong with it.
I suggest you enter the other required fields in the in your apex class.
Hope this helps,
Sandeep
I tried, but it’s not covered. Can u please update class & test class with required fields wherever required.
Thanks
public with sharing class CaseCreationWithQAttachment{
public Case caseData {get; set;}
public Attachment attachData {get; set;}
public CaseCreationWithQAttachment(ApexPages.StandardController stdCtr){
caseData = (Case) stdCtr.getRecord();
attachData = new Attachment();
}
public PageReference customSave(){
try{
insert caseData;
attachData.ParentId = caseData.Id;
insert attachData;
}
catch(Exception e){
ApexPages.addMessages(e);
}
PageReference p = new PageReference('/'+caseData.Id);
p.setRedirect(true);
return p;
}
}
@isTest
public class CaseCreationWithQAttachment_Test {
static testMethod void test1()
{
Case cs = new Case(Subject = 'Test', Origin = 'Phone', Status = 'New');
insert cs;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=cs.id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:cs.id];
System.assertEquals(1, attachments.size());
ApexPages.StandardController stdCtr = new ApexPages.StandardController(cs);
CaseCreationWithQAttachment ctr = new CaseCreationWithQAttachment(stdCtr);
ctr.caseData = cs;
ctr.attachData = attach;
ctr.customSave();
}
}
Below code is not covered in apex class: 86%
attachData.ParentId = caseData.Id;
insert attachData;
Controller
Test Class
What is use of If(Test.isRunningTest()) Integer i = 10/0; in class ? and
we are not inserting case & attachment in test class. these will be inserted while calling ctr.customSave() method in test class. right ?
This code is having 100% test coverage
Code Coverage
The Parent Id of case is getting associated automatically since you are using standard controller.
Please refer this blog to know how t o insert attachment from VF Page: http://salesforceworld.blogspot.com/2011/06/save-attachment-in-apex.html
Hope this helps,
Sandeep
Please mark as best answer if this helps
is there a chance you can help me out I am trying to attach and PDF from an VF Page to my quote record.
It quite works fine in the sandbox, but cant get it covered more than 36% by test class.
here is my class:
and here is the test I wrote by now:
I realy hope to get it running in my production.
kind regards
Thomas