You need to sign in to do that
Don't have an account?
salesforcerrr
Test class for Attachment Controller
Hi,
I am getting 25% coverage for below test class. I also copied the controller. Could someone assist with this please? Much appreciated.
I am getting 25% coverage for below test class. I also copied the controller. Could someone assist with this please? Much appreciated.
Public Class CaseImageController { String recId; public CaseImageController (ApexPages.StandardController controller) { recId = controller.getId(); } public String getFileId() { String fileId = ''; List<Attachment> attachedFiles = [select Id from Attachment where parentId =:recId order By LastModifiedDate DESC limit 1]; if( attachedFiles != null && attachedFiles.size() > 0 ) { fileId = attachedFiles[0].Id; } return fileId; } }
@isTest private class TestImageControllerTest{ static testMethod void testAttachments() { Case cc=new Case(Subject='Acme Inc'); insert cc; CaseImageController controller=new CaseImageController(new ApexPages.StandardController(cc)); Attachment attach=new Attachment(); attach.Name='Test'; Blob bodyBlob=Blob.valueOf('Testing Body of Attachment'); attach.body=bodyBlob; attach.parentId=cc.id; insert attach; List<Attachment> attachments=[select id, name from Attachment where parent.id=:cc.id]; System.assertEquals(1, attachments.size()); } }
--------------------------------------
@isTest
private class TestImageControllerTest {
static testMethod void testAttachments() {
Case cc = new Case(Subject = 'Acme Inc');
insert cc;
CaseImageController controller = new CaseImageController(new ApexPages.StandardController(cc));
Attachment attach = new Attachment();
attach.Name = 'Test';
Blob bodyBlob = Blob.valueOf('Testing Body of Attachment');
attach.body = bodyBlob;
attach.parentId = cc.id;
insert attach;
List < Attachment > attachments = [select id, name from Attachment where parent.id = : cc.id];
System.assertEquals(1, attachments.size());
// call your getFileId() to get code coverage
String newAttachId = controller.getFileId();
System.assertEquals(newAttachId, attachments[0].Id);
}
}
------------------------------------------
Regards,
Pawan Kumar