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
TemurifTemurif 

Test for Controller

Hello, 

I am new to apex and got this below code from Jeff Douglas' blog. Can you help for writing test code for below controller:
public class UploadAttachmentController2 {
    
    public String selectedType {get;set;}
    public Boolean selectedAwesomeness {get;set;}
    public String description {get;set;}
    private SFDC_Issue__c Issue {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentController2(ApexPages.StandardController controller) { 
        this.Issue = (SFDC_Issue__c)controller.getRecord();
    }   
    
    // creates a new Belgeler__c record
    private Database.SaveResult saveCustomAttachment() {
        Belgeler__c  obj = new Belgeler__c ();
        obj.Adim__c = Issue.Id; 
        obj.description__c = description;
   
        // fill out cust obj fields
        return Database.insert(obj);
    }
    
    // create an actual Attachment record with the Belgeler__c as parent
    private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = parentId;
        // inser the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    }
    
    /**
    * Upload process is:
    *  1. Insert new Belgeler__c record
    *  2. Insert new Attachment with the new Belgeler__c record as parent
    *  3. Update the Belgeler__c record with the ID of the new Attachment
    **/
    public PageReference processUpload() {
        try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
        
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));
                return null;
            }
        
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
        
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Belgeler__c customAttachment = [select id from Belgeler__c where id = :customAttachmentResult.getId()];
               
                customAttachment.Sonuc__c = attachmentResult.getId();
                update customAttachment;
            }
        
        } catch (Exception e) {
            ApexPages.AddMessages(e);
            return null;
        }
        
        return new PageReference('/'+Issue.Id);
    }
    
    public PageReference back() {
        return new PageReference('/'+Issue.Id);
    }     

}


Abhi_TripathiAbhi_Tripathi
Hi Temurif,

for the
For the test class of attachement go for this post
http://www.fishofprey.com/2011/10/inserting-attachment-in-apex-test.html

And for test class basics go for this one
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

Regards,
Abhi Tripathi
Salesforce Certfied Developer