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
AVINASH UPADHYAAVINASH UPADHYA 

Test Coverage Problem

Hi All, 

Bellow i have given my code. Bassically I am tryin to impliment an Upload button which submits the user entered data and Upload the file to Document folder.
 
public class AppFormExtender {
    private final College_Aid_Application_Form__c ClgAppForm;
  //  Public static College_Aid_Application_Form__c College_Aid_Application_Form{get;set;}
    private ApexPages.StandardController controller;
    // Public Id PageId;     
    Public AppFormExtender (apexpages.standardController controller){
    ClgAppForm = (College_Aid_Application_Form__c)controller.getRecord();
    this.controller = controller;       
 
 }   
 
 public Document document {
    get {
      if (document == null)
        document = new Document();
      return document;
    }
    set;
  }
    
public boolean confirmAgreement() {
    if(!ClgAppForm.Signature__c) {
        return false;
    } else {
        return true;
    }
}
   
     Public PageReference Upload(){
        if(confirmAgreement()){        
            document.AuthorId = UserInfo.getUserId();
            document.FolderId = UserInfo.getUserId(); // put it in running user's folder

         try {
    
    insert document;
    
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
      return null;
    }
            finally {
      document.body = null; // clears the viewstate
      document = new Document();
    }
          //Insert is commented to check the Upsert        
            Upsert ClgAppForm;
      //   PageReference acctPage = new ApexPages.StandardController(ClgAppForm).view();
       
       //PageReference FamilyDetail = new Apexpages.
     PageReference FamilyPage1 = Page.FamilyDetail;
     
            // FamilyPage1.getParameters().put('Family_Member_Detail__r' , ClgAppForm.Name);
             FamilyPage1.getParameters().put('PageId' , ClgAppForm.Id);
             FamilyPage1.setRedirect(true);
             return FamilyPage1;
         }else{
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please check all agreement checkboxes before submitting.'));
        return null;
         }

 }
}
 
@isTest
public class AppFormExtenderTestClass {
    @isTest static void TestSubmissionForm(){
        
        PageReference pageRef = Page.College_Aid_Application_Form;
        Test.setCurrentPage(pageRef);  
        College_Aid_Application_Form__c Application1 = new College_Aid_Application_Form__c(E_mail__c='Avinash@gmail.com', Age__c=25, Birthday__c = date.today(), Village__c = 'Test Village',Province__c='Test Province',Country__c='India',Zip_Code__c=543213,No_Years_to_graduate__c=10,Signature__c=True);
       
        ApexPages.standardController controller = new ApexPages.standardController(Application1);
        AppFormExtender extensions  = new AppFormExtender(controller);
        
        //Document Upload procee test
        Document document = new document(body=Blob.valueOf('Unit Test Document Body'), name='Test Calss Document');
        //Submission of Application form.
        extensions.Upload();
       //Redirecting to Family Detail Page.
String nextPage=extensions.Upload().getUrl(); 
 System.assertEquals('/apex/familydetail?PageId='+Application1.Id,nextPage);
    }
}
My expectation is bellow part of the test code will cover entire Upload methode functionality. But its not covering the bold lettered code in the above code.
extensions.Upload();
       //Redirecting to Family Detail Page.
String nextPage=extensions.Upload().getUrl(); 
 System.assertEquals('/apex/familydetail?PageId='+Application1.Id,nextPage);

Can any one please share your thoughts where i am going wrong.

Regards,
Avinash Upadhya
 
Best Answer chosen by AVINASH UPADHYA
Amit Chaudhary 8Amit Chaudhary 8

Try to use below code :-
 
@isTest
public class AppFormExtenderTestClass {
    @isTest static void TestSubmissionForm(){
        
        PageReference pageRef = Page.College_Aid_Application_Form;
        Test.setCurrentPage(pageRef);  
        College_Aid_Application_Form__c Application1 = new College_Aid_Application_Form__c(E_mail__c='Avinash@gmail.com', Age__c=25, Birthday__c = date.today(), Village__c = 'Test Village',Province__c='Test Province',Country__c='India',Zip_Code__c=543213,No_Years_to_graduate__c=10,Signature__c=True);
       
        ApexPages.standardController controller = new ApexPages.standardController(Application1);
        AppFormExtender extensions  = new AppFormExtender(controller);
        
		extensions.document.body = Blob.valueOf('Unit Test Document Body');
		extensions.document.name= 'Test Calss Document';
		extensions.Upload();

       //Redirecting to Family Detail Page.
		String nextPage=extensions.Upload().getUrl(); 
  	    System.assertEquals('/apex/familydetail?PageId='+Application1.Id,nextPage);
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help