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
Zoom_VZoom_V 

Test Code for a Visualforce controller

I am attempting to write test code for this controller. What I've written only covers 9% of the code. Can anybody tell what I'm doing wrong ? 

Here is the controller :
 
public class UploadAttachControllerVendProdRev {
    
    public String selectedType {get;set;}
    public Boolean selectedAwesomeness {get;set;}
    public String description {get;set;}
    private Vendor_Product_Review__c contact {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    public Vendor_Product_Review_Attachment__c objBVar{get;set;}
    
    public String contactproduct {get;set;}
    
    public UploadAttachControllerVendProdRev(ApexPages.StandardController controller) { 
        this.contact = (Vendor_Product_Review__c)controller.getRecord();
        
        objBVar = new Vendor_Product_Review_Attachment__c (Vendor_Product_Review__c = this.contact.id);
     
    }   
    
    // creates a new Vendor_Product_Review_Attachment__c record
    
    private Database.SaveResult saveCustomAttachment() {
   
                
        //Vendor_Product_Review_Attachment__c obj = new Vendor_Product_Review_Attachment__c();
        
        objBVar.Vendor_Product_Review__c = contact.Id; 
        objBVar.Vendor_Profile__c = contact.Vendor__c;
        objBVar.Vendor_Product__c = contact.Vendor_Product__c;
        objBVar.Description__c = description;
        objBVar.Document_type__c = selectedType;
       
       
        // fill out cust obj fields
        return Database.insert(objBVar);
        
    }
    
    // create an actual Attachment record with the Vendor_Product_Review_Attachment__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;
        // insert the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    }
    
    /**
    * Upload process is:
    *  1. Insert new Vendor_Product_Review_Attachment__c record
    *  2. Insert new Attachment with the new Vendor_Product_Review_Attachment__c record as parent
    *  3. Update the Vendor_Product_Review_Attachment__c record with the ID of the new Attachment
    **/
    public PageReference processUpload() {
    
        if(description =='' & filebody==null)
                {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                'You must include either a File or a Note.'));
                    return null;
                }
    
           
        /** if(description !='' & filebody==null)
                {        
                   Database.SaveResult customAttachmentResult = saveCustomAttachment();
                    
                // update the custom attachment record with some attachment info
                Vendor_Product_Review_Attachment__c customAttachment = [select id from Vendor_Product_Review_Attachment__c where id = :customAttachmentResult.getId()];                
                update customAttachment;
                }
         **/   
            
         try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
       
            if (!customAttachmentResult.isSuccess()) {
                //ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
                  //'Could not save attachment because either filebody was empty or the customAttachmentResult was unsuccessful.'));
                return null;
                system.debug('customattachmentresult **************!!! ');
                return new PageReference('/'+contact.Id);
            }
      
            if (filebody != 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;
                    return new PageReference('/'+contact.Id);
                } else {
                    // update the custom attachment record with some attachment info
                    Vendor_Product_Review_Attachment__c customAttachment = [select id from Vendor_Product_Review_Attachment__c where id = :customAttachmentResult.getId()];
                    customAttachment.name = this.fileName;
                    customAttachment.Attachment__c = attachmentResult.getId();
                    update customAttachment;
                }
            }
       } catch (Exception e) {
          ApexPages.AddMessages(e);
          return null;
       }

            return new PageReference('/'+contact.Id);
      }
        
    
    public PageReference back() {
        return new PageReference('/'+contact.Id);
    }     
}

Here is the test code I've written.
 
@isTest(SeeAllData = false)

private class TestUploadAttachControllerVendProdRev

    {    
        static testMethod void UnitTestUploadAttachControllerVendProdRev()
            
             {     
                Department__c dept = new Department__c();
                dept.Name = 'Dept1';
                dept.Department_Manager__c = '005i0000005o3zu';
                insert dept;                
                
                Vendor_Profile__c vprof = new Vendor_Profile__c();
                vprof.Name = 'ABCProf';
                vprof.Department_of_Record__c = dept.id;
                vprof.Department_Manager__c = '005i0000005o3zu';
                insert vprof;
                
                Vendor_Product__c vprod = new Vendor_Product__c();
                vprod.Name = 'ABCProd';
                vprod.Vendor__c = vprof.id;
                vprod.Reviewed_Vendor__c = vprof.id;
                vprof.Department_of_Record__c = dept.id;
                insert vprod;
                
                Vendor_Product__c vprod2 = new Vendor_Product__c();
                vprod2.Name = '123Prod';
                vprod2.Vendor__c = vprof.id;
                insert vprod2;
                
                Vendor_Contract__c vc = new Vendor_Contract__c();
                vc.Vendor__c = vprof.id;
                vc.Products_Included__c = '[ABCProd,123Prod]';
                insert vc ;
            
                Vendor_Product_Review__c vrev = new Vendor_Product_Review__c();
                vrev.Vendor__c = vprof.id;
                vrev.Vendor_Product__c = vprod.id;
                insert vrev ;
           
           
               ApexPages.StandardController sc = new ApexPages.standardController(vrev);
               UploadAttachControllerVendProdRev up = new UploadAttachControllerVendProdRev(sc);
          

             }
    
    }

Any help is appreciated. Thank you !
 
Best Answer chosen by Zoom_V
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
private class TestUploadAttachControllerVendProdRev
{    
	static testMethod void UnitTestUploadAttachControllerVendProdRev()
	{     
		Department__c dept = new Department__c();
		dept.Name = 'Dept1';
		dept.Department_Manager__c = '005i0000005o3zu';
		insert dept;                

		Vendor_Profile__c vprof = new Vendor_Profile__c();
		vprof.Name = 'ABCProf';
		vprof.Department_of_Record__c = dept.id;
		vprof.Department_Manager__c = '005i0000005o3zu';
		insert vprof;

		Vendor_Product__c vprod = new Vendor_Product__c();
		vprod.Name = 'ABCProd';
		vprod.Vendor__c = vprof.id;
		vprod.Reviewed_Vendor__c = vprof.id;
		vprof.Department_of_Record__c = dept.id;
		insert vprod;

		Vendor_Product__c vprod2 = new Vendor_Product__c();
		vprod2.Name = '123Prod';
		vprod2.Vendor__c = vprof.id;
		insert vprod2;

		Vendor_Contract__c vc = new Vendor_Contract__c();
		vc.Vendor__c = vprof.id;
		vc.Products_Included__c = '[ABCProd,123Prod]';
		insert vc ;

		Vendor_Product_Review__c vrev = new Vendor_Product_Review__c();
		vrev.Vendor__c = vprof.id;
		vrev.Vendor_Product__c = vprod.id;
		insert vrev ;

		
		ApexPages.StandardController sc = new ApexPages.standardController(vrev);
		UploadAttachControllerVendProdRev up = new UploadAttachControllerVendProdRev(sc);
		up.back();
		up.processUpload();
	}
	
	static testMethod void UnitTestUploadAttachControllerVendProdRe1()
	{     
		Department__c dept = new Department__c();
		dept.Name = 'Dept1';
		dept.Department_Manager__c = '005i0000005o3zu';
		insert dept;                

		Vendor_Profile__c vprof = new Vendor_Profile__c();
		vprof.Name = 'ABCProf';
		vprof.Department_of_Record__c = dept.id;
		vprof.Department_Manager__c = '005i0000005o3zu';
		insert vprof;

		Vendor_Product__c vprod = new Vendor_Product__c();
		vprod.Name = 'ABCProd';
		vprod.Vendor__c = vprof.id;
		vprod.Reviewed_Vendor__c = vprof.id;
		vprof.Department_of_Record__c = dept.id;
		insert vprod;

		Vendor_Product__c vprod2 = new Vendor_Product__c();
		vprod2.Name = '123Prod';
		vprod2.Vendor__c = vprof.id;
		insert vprod2;

		Vendor_Contract__c vc = new Vendor_Contract__c();
		vc.Vendor__c = vprof.id;
		vc.Products_Included__c = '[ABCProd,123Prod]';
		insert vc ;

		Vendor_Product_Review__c vrev = new Vendor_Product_Review__c();
		vrev.Vendor__c = vprof.id;
		vrev.Vendor_Product__c = vprod.id;
		insert vrev ;

		
		ApexPages.StandardController sc = new ApexPages.standardController(vrev);
		UploadAttachControllerVendProdRev up = new UploadAttachControllerVendProdRev(sc);
		up.description  ='Test';
		up.fileName ='Test';
		up.fileBody  = Blob.valueOf('Unit Test Attachment Body');
		up.processUpload();
	}
}
Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
private class TestUploadAttachControllerVendProdRev
{    
	static testMethod void UnitTestUploadAttachControllerVendProdRev()
	{     
		Department__c dept = new Department__c();
		dept.Name = 'Dept1';
		dept.Department_Manager__c = '005i0000005o3zu';
		insert dept;                

		Vendor_Profile__c vprof = new Vendor_Profile__c();
		vprof.Name = 'ABCProf';
		vprof.Department_of_Record__c = dept.id;
		vprof.Department_Manager__c = '005i0000005o3zu';
		insert vprof;

		Vendor_Product__c vprod = new Vendor_Product__c();
		vprod.Name = 'ABCProd';
		vprod.Vendor__c = vprof.id;
		vprod.Reviewed_Vendor__c = vprof.id;
		vprof.Department_of_Record__c = dept.id;
		insert vprod;

		Vendor_Product__c vprod2 = new Vendor_Product__c();
		vprod2.Name = '123Prod';
		vprod2.Vendor__c = vprof.id;
		insert vprod2;

		Vendor_Contract__c vc = new Vendor_Contract__c();
		vc.Vendor__c = vprof.id;
		vc.Products_Included__c = '[ABCProd,123Prod]';
		insert vc ;

		Vendor_Product_Review__c vrev = new Vendor_Product_Review__c();
		vrev.Vendor__c = vprof.id;
		vrev.Vendor_Product__c = vprod.id;
		insert vrev ;

		
		ApexPages.StandardController sc = new ApexPages.standardController(vrev);
		UploadAttachControllerVendProdRev up = new UploadAttachControllerVendProdRev(sc);
		up.back();
		up.processUpload();
	}
	
	static testMethod void UnitTestUploadAttachControllerVendProdRe1()
	{     
		Department__c dept = new Department__c();
		dept.Name = 'Dept1';
		dept.Department_Manager__c = '005i0000005o3zu';
		insert dept;                

		Vendor_Profile__c vprof = new Vendor_Profile__c();
		vprof.Name = 'ABCProf';
		vprof.Department_of_Record__c = dept.id;
		vprof.Department_Manager__c = '005i0000005o3zu';
		insert vprof;

		Vendor_Product__c vprod = new Vendor_Product__c();
		vprod.Name = 'ABCProd';
		vprod.Vendor__c = vprof.id;
		vprod.Reviewed_Vendor__c = vprof.id;
		vprof.Department_of_Record__c = dept.id;
		insert vprod;

		Vendor_Product__c vprod2 = new Vendor_Product__c();
		vprod2.Name = '123Prod';
		vprod2.Vendor__c = vprof.id;
		insert vprod2;

		Vendor_Contract__c vc = new Vendor_Contract__c();
		vc.Vendor__c = vprof.id;
		vc.Products_Included__c = '[ABCProd,123Prod]';
		insert vc ;

		Vendor_Product_Review__c vrev = new Vendor_Product_Review__c();
		vrev.Vendor__c = vprof.id;
		vrev.Vendor_Product__c = vprod.id;
		insert vrev ;

		
		ApexPages.StandardController sc = new ApexPages.standardController(vrev);
		UploadAttachControllerVendProdRev up = new UploadAttachControllerVendProdRev(sc);
		up.description  ='Test';
		up.fileName ='Test';
		up.fileBody  = Blob.valueOf('Unit Test Attachment Body');
		up.processUpload();
	}
}
Let us know if this will help you
 
This was selected as the best answer
Zoom_VZoom_V
Outstanding - thank you so much Amit ! I really learned a lot from this. I have never done a blob test before.