function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hemanth tanguturi 10hemanth tanguturi 10 

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>
                                                            &nbsp;&nbsp;
                                                            <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();
        }
    } 
}

 
Best Answer chosen by hemanth tanguturi 10
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
@isTest 
public class SF_AttachmentExecSummaryControllerTest 
{
	static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

		Attachment attach=new Attachment();   	
		attach.Name='Unit Test Attachment';
		Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
		attach.body=bodyBlob;
		attach.parentId=testAccount.id;
		insert attach;
    	
		
		Test.StartTest(); 

			ApexPages.currentPage().getParameters().put('Pid', String.valueOf(testAccount.Id));
			SF_AttachmentExecSummaryController  testAccPlan = new SF_AttachmentExecSummaryController();
			
			testAccPlan.attachment.body=bodyBlob;
			testAccPlan.attachment.Name='Unit Test Attachment1';
			testAccPlan.upload();	
			testAccPlan.deleteAttachmentId = attach.id;
			testAccPlan.DeleteAttach();

		Test.StopTest();
	}
}

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
@isTest 
public class SF_AttachmentExecSummaryControllerTest 
{
	static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

		Attachment attach=new Attachment();   	
		attach.Name='Unit Test Attachment';
		Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
		attach.body=bodyBlob;
		attach.parentId=testAccount.id;
		insert attach;
    	
		
		Test.StartTest(); 

			ApexPages.currentPage().getParameters().put('Pid', String.valueOf(testAccount.Id));
			SF_AttachmentExecSummaryController  testAccPlan = new SF_AttachmentExecSummaryController();
			
			testAccPlan.attachment.body=bodyBlob;
			testAccPlan.attachment.Name='Unit Test Attachment1';
			testAccPlan.upload();	
			testAccPlan.deleteAttachmentId = attach.id;
			testAccPlan.DeleteAttach();

		Test.StopTest();
	}
}

Let us know if this will help you
This was selected as the best answer
hemanth tanguturi 10hemanth tanguturi 10
Thanks Amit, Perfectly Worked.