You need to sign in to do that
Don't have an account?

test class for upload/Delete Attachment
Can someone help me with the test class for upload attachment and delete attachment, Bwlow is my vf page and controller:
VF Page: <tr class="rowPt10"> <td class="pb18 w20 textRight bold pr10 pl10" valign="top"> <apex:outputText value="Attachments"/> </td> <td class="pb18 greyText"> <apex:inputFile value="{!attachment.body}" fileName="{!attachment.name}" id="file"/> <apex:commandButton value="Upload" action="{!upload}" /> </td> </tr> <tr class="rowPt10"> <td class="pb18 greyText"> <apex:inputTextarea value="{!attachment.Description}"/> </td> </tr> <tr class="rowPt10"> <td colspan="3" class = "leftPadding" > <apex:dataTable value="{!myList}" var="attachmentRow"> <apex:column > <apex:commandLink action="{!DeleteAttach}" value="X" styleClass="btnSave fs8 btnXpadding GreyText left mr10 rc2" onclick="if(!confirm('Delete this attachment?')){event.preventDefault(); return false;};"> <apex:param name="deleteAttachmentId" value="{!attachmentRow.id}" assignTo="{!deleteAttachmentId}" /> </apex:commandLink> </apex:column> <apex:column > <apex:outputLink value="{!URLFOR($Action.Attachment.Download,attachmentRow.Id)}" target="$_"> {!attachmentRow.Name} </apex:outputLink> <apex:outputText value="[ By {!attachmentRow.CreatedBy.Name} on {!attachmentRow.CreatedDate}]"/> </apex:column> </apex:dataTable> </td> </tr> </table> Controller: public class SF_AttachmentExecSummaryController { public string Pid{get;set;} public list<attachment> myList{get; set;} Public string deleteAttachmentId{get;set;} public SF_AttachmentExecSummaryController(){ //Pid='a0o17000002AvZvAAK'; Pid =ApexPages.currentPage().getParameters().get('Pid'); system.debug('id:::::'+Pid); loadAttachments(); } public attachment attachment { get { if (attachment == null) attachment = new Attachment(); return attachment; } set; } public PageReference upload() { attachment.OwnerId = UserInfo.getUserId(); attachment.ParentId = Pid; // the record the file is attached to attachment.IsPrivate = true; try { insert attachment; } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment')); return null; } finally { attachment = new Attachment(); } loadAttachments(); ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully')); return null; } public void loadAttachments(){ myList = new list<attachment>();//h myList = [SELECT CreatedDate,Name,CreatedBy.Name FROM Attachment where parentid =:Pid];//h } public void DeleteAttach() { if (deleteAttachmentId!=null){ Attachment attachment = [SELECT Id, OwnerId, ParentId from attachment where Id = :deleteAttachmentId]; //MnPriceApprovalAttachment__c mnPriceApprovalAttachment = [Select Id from MnPriceApprovalAttachment__c where Id=:attachment.ParentId]; if(userinfo.getUserId()==attachment.OwnerId) { try{ delete attachment ; } catch (DMLException e) { System.debug('The following exception has occurred: ' + e.getMessage()); } } loadAttachments(); } } }
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Let us know if this will help you
All Answers
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Let us know if this will help you