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
Viasur-Viasur- 

Test Class for Extension Controller / Visualforce page

Hello all,

I have been working on this code for a couple of weeks and now the time has come to build the test class for it :( . I don't have much experience on writing test class to be honest, but now I know how painful this can be. I have tried a few things but I just can get the full understanding on how to start coding my test class for this code. I have done some test codes for triggers, but much smaller than this. I will really appreciated if someone can give me some directions. I'm totally lost.  I look my code and can't start thinking on how to get this done.

I hope someone can help me, thanks.
public with sharing class PictureAttachmentController {
       
   public Visit_Report__c vr = new Visit_Report__c();
       
   String recId;
  
   public boolean renOne { get; set; }
   public boolean renTwo { get; set; }
   public boolean renOneIS { get; set; }
   public boolean renTwoIS { get; set; }
   public boolean displaySavedPopup {get; set;}
   public boolean refreshPage {get; set;}
   public String fileIdName {get; set;}
   public String fileIdName2 {get; set;}
   public String fileIdNameIS {get; set;}
   public String fileIdNameIS2 {get; set;}
   private ApexPages.StandardController stdController;
   private PageReference page; 
   
   public String errorMessage {get; set;} 
   public boolean hasErrors = false;
   public boolean getHasErrors(){
       return hasErrors;
    
  }
   
      
   public PictureAttachmentController(ApexPages.StandardController controller) {
   
   this.vr =(Visit_Report__c)controller.getRecord(); 
   this.stdController = controller;
   this.page = ApexPages.currentPage();
        
   recId = controller.getId(); 
   refreshPage= false;
   
     
  }
  
                  
   public void closePopup() {
       displaySavedPopup = false;
       
        
  }
  
 
   public void showPopup() {
       displaySavedPopUp = true; 
      
            
  }
  
          
   public Attachment pictureOne {
       get {
          if (pictureOne == null)
          pictureOne = new Attachment();
          return pictureOne;
           }
      set;
      
   }
 
    
   public PageReference savePictureOne() {
 
    pictureOne.OwnerId = UserInfo.getUserId();
    pictureOne.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureOne.Name = 'W1' + pictureOne.name; 
     
    
    try {
      
      insert pictureOne;
      renOne = true;
      
      
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading picture one'));
      return null;
    } finally {
      pictureOne = new Attachment(); 
            
            
    }
 
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Picture one uploaded successfully - Please wait, refreshing page'));
    refreshPage =true;
    return null;
    
    }    
    
    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [SELECT Id, Name FROM Attachment WHERE Name LIKE 'W1%' AND parentId =:recId ORDER By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
            fileIdName = attachedFiles[0].Name; 
        }
        return fileId;    
    }
    
    public Attachment pictureTwo {
       get {
          if (pictureTwo == null)
          pictureTwo = new Attachment();
          return pictureTwo;
           }
       set;
    }
    
    public PageReference savePictureTwo() {
 
    pictureTwo.OwnerId = UserInfo.getUserId();
    pictureTwo.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureTwo.Name = 'W2' + pictureTwo.name;
    
    try {
      
      insert pictureTwo;
      renTwo = true;
      
      
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading picture two'));
      return null;
    } finally {
      pictureTwo = new Attachment(); 
       
       
    }
 
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Picture two uploaded successfully - Please wait, refreshing page'));
    refreshPage =true;
    return null;
    
    }    
    
    public String getFileId2() {
        String fileId2 = '';
        List<Attachment> attachedFiles = [SELECT Id, Name FROM Attachment WHERE Name LIKE 'W2%' AND parentId =:recId ORDER By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId2 = attachedFiles[0].Id;
            fileIdName2 = attachedFiles[0].Name;
        }
        return fileId2;    
    }
    
// Identical process for In-Store pictures section


   public Attachment pictureOneInStore {
       get {
          if (pictureOneInStore == null)
          pictureOneInStore = new Attachment();
          return pictureOneInStore;
           }
      set;
      
   }
 
    
   public PageReference savePictureOneInStore() {
 
    pictureOneInStore.OwnerId = UserInfo.getUserId();
    pictureOneInStore.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureOneInStore.Name = 'IS1' + pictureOneInStore.name; 
    
    try {
      
      insert pictureOneInStore;
      renOneIS = true;
      
      
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading picture one'));
      return null;
    } finally {
      pictureOneInStore = new Attachment(); 
       
       
    }
 
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Picture one uploaded successfully - Please wait, refreshing page'));
    refreshPage =true;
    return null;
    
    }    
    
    public String getFileIdIS() {
        String fileIdIS = '';
        List<Attachment> attachedFiles = [SELECT Id, Name FROM Attachment WHERE Name LIKE 'IS1%' AND parentId =:recId ORDER By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileIdIS = attachedFiles[0].Id;
            fileIdNameIS = attachedFiles[0].Name;
        }
        return fileIdIS;    
    }
    
    public Attachment pictureTwoInStore {
       get {
          if (pictureTwoInStore == null)
          pictureTwoInStore = new Attachment();
          return pictureTwoInStore;
           }
       set;
    }
    
    public PageReference savePictureTwoInStore() {
 
    pictureTwoInStore.OwnerId = UserInfo.getUserId();
    pictureTwoInStore.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureTwoInStore.Name = 'IS2' + pictureTwoInStore.name;
    
    try {
      
      insert pictureTwoInStore;
      renTwoIS = true;
      
      
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading picture two'));
      return null;
    } finally {
      pictureTwoInStore = new Attachment(); 
       
       
    }
 
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Picture two uploaded successfully - Please wait, refreshing page'));
    refreshPage =true;
    return null;
    
    }    
    
    public String getFileIdIS2() {
        String fileIdIS2 = '';
        List<Attachment> attachedFiles = [SELECT Id, Name FROM Attachment WHERE Name LIKE 'IS2%' AND parentId =:recId ORDER By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileIdIS2 = attachedFiles[0].Id;
            fileIdNameIS2 = attachedFiles[0].Name;
        }
        return fileIdIS2;    
    }
    
    
    // Save methods
    
    
    public PageReference save(){
       hasErrors=false;
       
       Visit_Report__c compFields =[SELECT id, Description_Photo_One_Window__c, Description_Photo_Two_Window__c FROM Visit_Report__c WHERE Id =:recId];
 
    
       if(compFields.Description_Photo_One_Window__c == null && vr.Description_Photo_One_Window__c ==null && compFields.Description_Photo_Two_Window__c == null && vr.Description_Photo_Two_Window__c ==null){
          hasErrors=true;
                  
 }          
       if(hasErrors){
       errorMessage='No description have been inserted for Window/Facade section!';
       return null;        
       
 }else {              
       
       this.stdController.save(); 
       displaySavedPopUp =true;
       errorMessage='';
       refreshPage = true;
       return null;
       } 
             
  }
  
   public PageReference save2(){
       hasErrors=false;
       
       Visit_Report__c descFieldsInStore = [SELECT id, Description_Photo_One_In_Store__c, Description_Photo_Two_In_Store__c FROM Visit_Report__c WHERE Id =:recId];                           
  
       if(descFieldsInStore.Description_Photo_One_In_Store__c == null && vr.Description_Photo_One_In_Store__c ==null && descFieldsInStore.Description_Photo_Two_In_Store__c == null && vr.Description_Photo_Two_In_Store__c ==null){
          hasErrors=true;
                  
 }          
       if(hasErrors){
       errorMessage='No description have been inserted for In-Store section!';
       return null;        
       
 }else {              
       
       this.stdController.save(); 
       displaySavedPopUp =true;
       errorMessage='';
       refreshPage = true;
       return null;
       } 
             
   }

}


PrasanntaPrasannta (Salesforce Developers) 
Hi,

Please refer to this link below to get basic understanding on how to write the test class-

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

Hope it helps.
Viasur-Viasur-
Hello there, Thanks for your reply. I know how to write those types of test classes. Like I mentioned in my first entry, I do have experience on trigger testing. This code that I'm exposing here it is a bit more complex. It use an extension controller and.a visualforce page.