You need to sign in to do that
Don't have an account?
Nitesh Trivedi
can any one help me to write the test class of below code
public class IncludeDocumentController {
//public blob attach{get;set;}
public integer displayOrder{get;set;}
public Additional_Document__c addattach{get;set;}
public string recordId{get;set;}
public string quoteNum{get;set;}
public Quote quotes{get;set;}
public list<Quote> listquote{get;set;}
public Attachment myfile;
ApexPages.StandardController sc;
//includedocment controller
public IncludeDocumentController(ApexPages.StandardController sc){
this.sc = sc;
listquote=new list<Quote>();
recordId=ApexPages.CurrentPage().getparameters().get('id');
for(Quote q:[select id,QuoteNumber from Quote where id=:recordId]){
quoteNum=q.QuoteNumber;
}
}
//get attched file
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
// method to save file and redirect to quote page
public pageReference save(){
Attachment a = new Attachment(parentId = recordId, name=myfile.name, body = myfile.body);
/*addattach=new Additional_Document__c();
addattach.Name=myfile.name;
addattach.External_Id__c=a.id;
if(displayOrder!=null){
addattach.Display_Order__c=displayOrder;
}
addattach.Quote__c=recordId;*/
try {
insert a;
addattach=new Additional_Document__c();
addattach.Name=myfile.name;
addattach.External_Id__c=a.id;
if(displayOrder!=null){
addattach.Display_Order__c=displayOrder;
}
addattach.Quote__c=recordId;
insert addattach;
}
catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
}
pageReference pr = new PageReference('/' + recordId );
pr.setRedirect(true);
if(myfile!=null){
return pr;
}else{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Attach File'));
}
return null;
}
//cancel method to redirect to quote page
public pageReference cancel(){
pageReference pr = new PageReference('/' + recordId );
pr.setRedirect(true);
return pr;
}
/* //importline page import button method
public pageReference importLine(){
pageReference pr = new PageReference('/' + recordId );
pr.setRedirect(true);
return pr;
}*/
}
//public blob attach{get;set;}
public integer displayOrder{get;set;}
public Additional_Document__c addattach{get;set;}
public string recordId{get;set;}
public string quoteNum{get;set;}
public Quote quotes{get;set;}
public list<Quote> listquote{get;set;}
public Attachment myfile;
ApexPages.StandardController sc;
//includedocment controller
public IncludeDocumentController(ApexPages.StandardController sc){
this.sc = sc;
listquote=new list<Quote>();
recordId=ApexPages.CurrentPage().getparameters().get('id');
for(Quote q:[select id,QuoteNumber from Quote where id=:recordId]){
quoteNum=q.QuoteNumber;
}
}
//get attched file
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
// method to save file and redirect to quote page
public pageReference save(){
Attachment a = new Attachment(parentId = recordId, name=myfile.name, body = myfile.body);
/*addattach=new Additional_Document__c();
addattach.Name=myfile.name;
addattach.External_Id__c=a.id;
if(displayOrder!=null){
addattach.Display_Order__c=displayOrder;
}
addattach.Quote__c=recordId;*/
try {
insert a;
addattach=new Additional_Document__c();
addattach.Name=myfile.name;
addattach.External_Id__c=a.id;
if(displayOrder!=null){
addattach.Display_Order__c=displayOrder;
}
addattach.Quote__c=recordId;
insert addattach;
}
catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
}
pageReference pr = new PageReference('/' + recordId );
pr.setRedirect(true);
if(myfile!=null){
return pr;
}else{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Attach File'));
}
return null;
}
//cancel method to redirect to quote page
public pageReference cancel(){
pageReference pr = new PageReference('/' + recordId );
pr.setRedirect(true);
return pr;
}
/* //importline page import button method
public pageReference importLine(){
pageReference pr = new PageReference('/' + recordId );
pr.setRedirect(true);
return pr;
}*/
}
The below articles give a good insight into how to begin writing test class and how coverage can be improved
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Examples: https://developer.salesforce.com/forums/?id=9060G000000XiuOQAS
https://salesforce.stackexchange.com/questions/138569/unit-test-for-attachment-upload-vf-page
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you