You need to sign in to do that
Don't have an account?

Test class for Contoller's method which involve attachment insert
Hello, can anyone help me in this question, in this method i am some custom record as well as attachment, my test class is running for custom records but it gives error "System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]"
this is method's code ,
Public PageReference saveRecordlprop(){
lead.Labor_Mid_Construction_Proposal_Added__c = true ;
upsert lead;
update testlprop ;
lprop.Marketing_Material__c = testlprop.Marketing_Material__c;
lprop.Holiday_Coverage__c = testlprop.Holiday_Coverage__c;
lprop.Management_and_Supervision__c = testlprop.Management_and_Supervision__c ;
lprop.Over_Time_Hours__c = testlprop.Over_Time_Hours__c ;
lprop.Payment_Terms__c = testlprop.Payment_Terms__c ;
lprop.Pricing__c = testlprop.Pricing__c ;
lprop.Statement_of_Work__c = testlprop.Statement_of_Work__c ;
lprop.Supplies_and_Equipment__c = testlprop.Supplies_and_Equipment__c ;
lprop.Contract_Terms__c = testlprop.Contract_Terms__c;
insert lprop;
lpdocument = new Attachment();
lpdocument.Name = 'Client cover image';
lpdocument.ParentId = lprop.ID;
lpdocument.Body = lpfileBody;
upsert lpdocument;
lprop.Current_Record_ID__c = lpdocument.ID;
upsert lprop;
PageReference p = new PageReference('/' + this.lprop.Id );
return p;
}
and this is my test class for this controller
@isTest
public class addProposalCntrltest2{
static testMethod void testMethod1() {
Lead testRecord= new Lead();
testRecord.LastName = 'test name ';
testRecord.Company = 'test company';
testRecord.Status = 'new';
testRecord.Credit_Limit_Amount__c = 12344;
insert testRecord;
Laborer_Mid_Construction_Proposal__c testRecord2 = new Laborer_Mid_Construction_Proposal__c();
testRecord2.Name = 'test';
testRecord2.Lead__c= testRecord.ID;
insert testRecord2 ;
Blob lpfileBody = Blob.valueOf('Test Data');
Attachment attachment = new Attachment();
attachment.Name = 'Test Attachment for Parent';
attachment.ParentId = testRecord2.ID;
attachment.Body = lpfileBody;
insert attachment;
Steady_Maintenance_Proposal__c testRecord3 = new Steady_Maintenance_Proposal__c();
testRecord3.Name = 'test';
testRecord3.Lead__c= testRecord.ID;
insert testRecord3;
Post_Construction_Cleanup_Proposal__c testRecord4 = new Post_Construction_Cleanup_Proposal__c();
testRecord4.Name = 'test';
testRecord4.Lead__c= testRecord.ID;
insert testRecord4 ;
Steady_Maintenance_Commercial__c testRecord5 = new Steady_Maintenance_Commercial__c();
testRecord5.Name = 'test';
testRecord5.Lead__c= testRecord.ID;
Insert testRecord5;
Steady_Maintenance_Residential__c testRecord6 = new Steady_Maintenance_Residential__c();
testRecord6.Name = 'test';
testRecord6.Lead__c= testRecord.ID;
insert testRecord6 ;
Floor_Cleaning_Project_Over_View__c testRecord7 = new Floor_Cleaning_Project_Over_View__c();
testRecord7.Name = 'test';
testRecord7.Lead__c= testRecord.ID;
insert testRecord7;
General_PCC_Project_Over_View__c testRecord8 = new General_PCC_Project_Over_View__c();
testRecord8.Name = 'test';
testRecord8.Lead__c= testRecord.ID;
insert testRecord8;
Window_Cleaning_Project_Overview__c testRecord9 = new Window_Cleaning_Project_Overview__c();
testRecord9.Name = 'test';
testRecord9.Lead__c= testRecord.ID;
insert testRecord9 ;
Test.setCurrentPage(Page.addProposal);
System.currentPageReference().getParameters().put('id', testRecord.Id);
addProposalCntrl addProposalCntrlTestIns = new addProposalCntrl();
addProposalCntrlTestIns.saveRecordlprop();
addProposalCntrlTestIns.saveRecordspropComm();
addProposalCntrlTestIns.saveRecordspropResd();
addProposalCntrlTestIns.saveRecordpprop1();
addProposalCntrlTestIns.saveRecordpprop2();
addProposalCntrlTestIns.saveRecordpprop3();
}
private static void addAttachmentToParent(Id testRecord) {
}
}
Thank you!
this is method's code ,
Public PageReference saveRecordlprop(){
lead.Labor_Mid_Construction_Proposal_Added__c = true ;
upsert lead;
update testlprop ;
lprop.Marketing_Material__c = testlprop.Marketing_Material__c;
lprop.Holiday_Coverage__c = testlprop.Holiday_Coverage__c;
lprop.Management_and_Supervision__c = testlprop.Management_and_Supervision__c ;
lprop.Over_Time_Hours__c = testlprop.Over_Time_Hours__c ;
lprop.Payment_Terms__c = testlprop.Payment_Terms__c ;
lprop.Pricing__c = testlprop.Pricing__c ;
lprop.Statement_of_Work__c = testlprop.Statement_of_Work__c ;
lprop.Supplies_and_Equipment__c = testlprop.Supplies_and_Equipment__c ;
lprop.Contract_Terms__c = testlprop.Contract_Terms__c;
insert lprop;
lpdocument = new Attachment();
lpdocument.Name = 'Client cover image';
lpdocument.ParentId = lprop.ID;
lpdocument.Body = lpfileBody;
upsert lpdocument;
lprop.Current_Record_ID__c = lpdocument.ID;
upsert lprop;
PageReference p = new PageReference('/' + this.lprop.Id );
return p;
}
and this is my test class for this controller
@isTest
public class addProposalCntrltest2{
static testMethod void testMethod1() {
Lead testRecord= new Lead();
testRecord.LastName = 'test name ';
testRecord.Company = 'test company';
testRecord.Status = 'new';
testRecord.Credit_Limit_Amount__c = 12344;
insert testRecord;
Laborer_Mid_Construction_Proposal__c testRecord2 = new Laborer_Mid_Construction_Proposal__c();
testRecord2.Name = 'test';
testRecord2.Lead__c= testRecord.ID;
insert testRecord2 ;
Blob lpfileBody = Blob.valueOf('Test Data');
Attachment attachment = new Attachment();
attachment.Name = 'Test Attachment for Parent';
attachment.ParentId = testRecord2.ID;
attachment.Body = lpfileBody;
insert attachment;
Steady_Maintenance_Proposal__c testRecord3 = new Steady_Maintenance_Proposal__c();
testRecord3.Name = 'test';
testRecord3.Lead__c= testRecord.ID;
insert testRecord3;
Post_Construction_Cleanup_Proposal__c testRecord4 = new Post_Construction_Cleanup_Proposal__c();
testRecord4.Name = 'test';
testRecord4.Lead__c= testRecord.ID;
insert testRecord4 ;
Steady_Maintenance_Commercial__c testRecord5 = new Steady_Maintenance_Commercial__c();
testRecord5.Name = 'test';
testRecord5.Lead__c= testRecord.ID;
Insert testRecord5;
Steady_Maintenance_Residential__c testRecord6 = new Steady_Maintenance_Residential__c();
testRecord6.Name = 'test';
testRecord6.Lead__c= testRecord.ID;
insert testRecord6 ;
Floor_Cleaning_Project_Over_View__c testRecord7 = new Floor_Cleaning_Project_Over_View__c();
testRecord7.Name = 'test';
testRecord7.Lead__c= testRecord.ID;
insert testRecord7;
General_PCC_Project_Over_View__c testRecord8 = new General_PCC_Project_Over_View__c();
testRecord8.Name = 'test';
testRecord8.Lead__c= testRecord.ID;
insert testRecord8;
Window_Cleaning_Project_Overview__c testRecord9 = new Window_Cleaning_Project_Overview__c();
testRecord9.Name = 'test';
testRecord9.Lead__c= testRecord.ID;
insert testRecord9 ;
Test.setCurrentPage(Page.addProposal);
System.currentPageReference().getParameters().put('id', testRecord.Id);
addProposalCntrl addProposalCntrlTestIns = new addProposalCntrl();
addProposalCntrlTestIns.saveRecordlprop();
addProposalCntrlTestIns.saveRecordspropComm();
addProposalCntrlTestIns.saveRecordspropResd();
addProposalCntrlTestIns.saveRecordpprop1();
addProposalCntrlTestIns.saveRecordpprop2();
addProposalCntrlTestIns.saveRecordpprop3();
}
private static void addAttachmentToParent(Id testRecord) {
}
}
Thank you!
for Example :
Controller Class :
Public Class AttachwithBody {
Public Blob lpfileBody {get;set;} // Class Variable
public Id ParentId;
public void savewithBody () {
Attachment attachment = new Attachment();
attachment.Name = 'Attachment name';
attachment.ParentId = ParentId;
attachment.Body = lpfileBody;
insert attachment;
}
}
Test Class :
@isTest
public Class AttachwithBodyTest {
static testMethod void testMethod1() {
AttachwithBody awB = new AttachwithBody(); // Create the Instance of Class AttachwithBody
awB.lpfileBody = Blob.valueOf('Test'); // set the value of lpfileBody Class Variable
awB.savewithBody(); // Call the Class Method savewithBody()
}
}
Thanks
Ahmed Ansari
All Answers
Hi Chaim Rosenfeld 8,
as i see you have not set the value of Class variable lpfileBody in your Test class please set the value i think it resolves your problem.
Thanks
Ahmed Ansari
I have here is snippet ,
Hi Chaim Rosenfeld 8,
in above sharing snippet lpfileBody is your test class variable its not your Controller class method i mean to say you can set the value of Controller Class Variable named lpfileBody with the instance of Controller class .
Thanks
Ahmed Ansari
can you please send me some example, i am not getting it
Thank you!
for Example :
Controller Class :
Public Class AttachwithBody {
Public Blob lpfileBody {get;set;} // Class Variable
public Id ParentId;
public void savewithBody () {
Attachment attachment = new Attachment();
attachment.Name = 'Attachment name';
attachment.ParentId = ParentId;
attachment.Body = lpfileBody;
insert attachment;
}
}
Test Class :
@isTest
public Class AttachwithBodyTest {
static testMethod void testMethod1() {
AttachwithBody awB = new AttachwithBody(); // Create the Instance of Class AttachwithBody
awB.lpfileBody = Blob.valueOf('Test'); // set the value of lpfileBody Class Variable
awB.savewithBody(); // Call the Class Method savewithBody()
}
}
Thanks
Ahmed Ansari