You need to sign in to do that
Don't have an account?
Salvatore 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);
}
}
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);
}
}
Let us know if this will help u
All Answers
Let us know if this will help u