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
Salvatore GomezSalvatore Gomez 

Unit test for attachment

I seem to be struggling with my unit testing for this class. I can't seem to get past a 57% code coverage with my current unit test so I know i'm missing something but i just can't figure it out. Everything in bold in the unit test below is not being tested and i have tried multiple scenarios that just don't seem to cover it. I'm wondering if re-working the class would be a better option. Any thoughts or suggestions? Thank you.

public class InputFileControllerExtension {
 
   private final Job_Applicants__c app;
   public Attachment attachment {get;set;}  
   public PageReference save() {
   
     try{
     insert app;
     attachment.parentid = app.id;
     insert attachment;
     return stdController.save();

 
     
     PageReference confirmPage = Page.GA_ApplicantConfirmation;
     confirmPage.setRedirect(true);
     return confirmPage;

     }Catch(DmlException e){
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'an account has already been created for that ASURITE');
                ApexPages.addMessage(myMsg);

     
     }
     Catch(Exception e){
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'an error has occurred please contact wpcarey.support@asu.edu');
                ApexPages.addMessage(myMsg);

     }
        Return null;
   
   }
   
   
 
 public InputFileControllerExtension(ApexPages.StandardController stdController)
 {
 
  attachment = new Attachment();
  this.app = (Job_Applicants__c)stdController.getRecord();
  this.stdController = stdController;
 }
 
 ApexPages.StandardController stdController;
}

=========================================
@isTest
private class InputTest {

    static testMethod void InputFileTest() {
    
    Job_Applicants__c Applicant = new Job_Applicants__c();
    
    Applicant.ASURITE__c='Test';
    
    Insert Applicant;
    
    Attachment attach=new Attachment();    
    attach.Name='Unit Test Attachment';
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    attach.body=bodyBlob;
    attach.parentId=Applicant.id;
    attach.ContentType = 'application/msword';
    attach.IsPrivate = false;
    attach.Description = 'Test';
    insert attach;
    
    Attachment testAtt = [Select ParentId from Attachment where Id =:attach.Id];
    
    System.assertEquals(Applicant.Id,testAtt.ParentId);
 
    Test.startTest();
    ApexPages.StandardController sc = new ApexPages.StandardController(Applicant);
    InputFileControllerExtension Inp = new InputFileControllerExtension(sc);
    PageReference pageRef = Page.GA_CreateApplicant; // Add your VF page Name here
    pageRef.getParameters().put('id', String.valueOf(Applicant.Id));
    Test.setCurrentPage(pageRef);
    Inp.save();
    Test.stopTest();
    
    System.assertEquals(testAtt, Inp.attachment);
    
        
    }
}
Best Answer chosen by Salvatore Gomez
Amit Chaudhary 8Amit Chaudhary 8
Please try to update your test class like below
@isTest
private class InputTest {

    static testMethod void InputFileTest() 
	{
    
		Job_Applicants__c Applicant = new Job_Applicants__c();
		Applicant.ASURITE__c='Test';
		
		Attachment attach=new Attachment();    
		attach.Name='Unit Test Attachment';
		Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
		attach.body=bodyBlob;
		attach.ContentType = 'application/msword';
		attach.IsPrivate = false;
		attach.Description = 'Test';
		
		Test.startTest();
		
			ApexPages.StandardController sc = new ApexPages.StandardController(Applicant);
			InputFileControllerExtension Inp = new InputFileControllerExtension(sc);
			Inp.attachment = attach;
			Inp.save();
			Test.stopTest();
		
		System.assertEquals(testAtt, Inp.attachment);
    
        
    }
}

Let us know if this will help u

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try to update your test class like below
@isTest
private class InputTest {

    static testMethod void InputFileTest() 
	{
    
		Job_Applicants__c Applicant = new Job_Applicants__c();
		Applicant.ASURITE__c='Test';
		
		Attachment attach=new Attachment();    
		attach.Name='Unit Test Attachment';
		Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
		attach.body=bodyBlob;
		attach.ContentType = 'application/msword';
		attach.IsPrivate = false;
		attach.Description = 'Test';
		
		Test.startTest();
		
			ApexPages.StandardController sc = new ApexPages.StandardController(Applicant);
			InputFileControllerExtension Inp = new InputFileControllerExtension(sc);
			Inp.attachment = attach;
			Inp.save();
			Test.stopTest();
		
		System.assertEquals(testAtt, Inp.attachment);
    
        
    }
}

Let us know if this will help u
This was selected as the best answer
Salvatore GomezSalvatore Gomez
Thank you very much Amit this did solve my issue.