You need to sign in to do that
Don't have an account?
laurent.greiwelding1.3974669707216677E12
Can any one help me writing test class for a class ?
I have this class :
and th test class :
The code coverage is 26%, can you help me please
public with sharing class PdfGeneratorController { public PdfGeneratorController(ApexPages.StandardController controller) { } public ID parentId {get;set;} public String pdfName {get;set;} public ID factureId {get;set;} public PageReference savePdf() { Facture__c f = [Select Devis__c, Name From Facture__c Where Id = :ApexPages.currentPage().getParameters().get('id')]; factureId = ApexPages.currentPage().getParameters().get('id'); pdfName = 'Facture '+f.Name; parentId = f.Devis__c; PageReference pdf = Page.Facture_Puerto_cacao_pdf; // add parent id to the parameters for standardcontroller pdf.getParameters().put('id',parentId); // create the new attachment Attachment attach = new Attachment(); // the contents of the attachment from the pdf Blob body; try { // returns the output of the page as a PDF body = pdf.getContent(); // need to pass unit test -- current bug } catch (VisualforceException e) { body = Blob.valueOf('Some Text'); } attach.Body = body; // add the user entered name attach.Name = pdfName; attach.IsPrivate = false; // attach the pdf to the account attach.ParentId = factureId; insert attach; // send the user to the account to view results return new PageReference('/'+factureId); } }
and th test class :
@isTest private class Test_PdfGeneratorController { static Facture__c fact; static { fact = new Facture__c(); fact.Devis__c = '0Q0L0000000AH4m'; insert fact; } static testMethod void testPdfGenerator() { PageReference pref = Page.Facture_Puerto_cacao_pdf; pref.getParameters().put('id',fact.Devis__c); Test.setCurrentPage(pref); // Instantiate standard Controller ApexPages.standardController controller = new ApexPages.standardController(fact); // Instantiate controller extension PdfGeneratorController con = new PdfGeneratorController(controller); Test.startTest(); // populate the field with values con.parentId = fact.Id; con.pdfName = 'My Test PDF'; // submit the record pref = con.savePdf(); // assert that they were sent to the correct page System.assertEquals(pref.getUrl(),'/'+fact.id); // assert that an attachment exists for the record System.assertEquals(1,[select count() from attachment where parentId = :fact.Devis__c]); Test.stopTest(); } }
The code coverage is 26%, can you help me please
Modify your code as below in PdfGeneratorController and leave test class as it is. You should fix other syntax errors yourself.
public with sharing class PdfGeneratorController {
public PdfGeneratorController(ApexPages.StandardController controller) {
}
public ID parentId {get;set;}
public String pdfName {get;set;}
public ID factureId {get;set;}
public PageReference savePdf() {
if (Test.IsRunningTest()){
Facture__c fact = new Facture__c();
fact.Devis__c = '0Q0L0000000AH4m';
insert fact;
PageReference pref = Page.Facture_Puerto_cacao_pdf;
pref.getParameters().put('id',fact.Devis__c);
Test.setCurrentPage(pref);
factureId = fact.Devis__c;
pdfName = 'Facture '+fact.Name;
parentId = fact.Devis__c;
}else{
Facture__c f = [Select Devis__c, Name From Facture__c Where Id = :ApexPages.currentPage().getParameters().get('id')];
factureId = ApexPages.currentPage().getParameters().get('id');
pdfName = 'Facture '+f.Name;
parentId = f.Devis__c;
}
PageReference pdf = Page.Facture_Puerto_cacao_pdf;
// add parent id to the parameters for standardcontroller
pdf.getParameters().put('id',parentId);
// create the new attachment
Attachment attach = new Attachment();
// the contents of the attachment from the pdf
Blob body;
try {
// returns the output of the page as a PDF
body = pdf.getContent();
// need to pass unit test -- current bug
} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
}
attach.Body = body;
// add the user entered name
attach.Name = pdfName;
attach.IsPrivate = false;
// attach the pdf to the account
attach.ParentId = factureId;
insert attach;
// send the user to the account to view results
return new PageReference('/'+factureId);
}
}
Thanks,
Grazitti Team.
All Answers
Can you paste those lines here?
Please modify the code as follows in Test_PdfGeneratorController :
if (Test.IsRunningTest()){
Facture__c fact = new Facture__c();
fact.Devis__c = '0Q0L0000000AH4m';
insert fact;
PageReference pref = Page.Facture_Puerto_cacao_pdf;
pref.getParameters().put('id',fact.Devis__c);
Test.setCurrentPage(pref);
factureId = fact.Devis__c;
pdfName = 'Facture '+fact.Name;
parentId = fact.Devis__c;
}else{
Facture__c f = [Select Devis__c, Name From Facture__c Where Id = :ApexPages.currentPage().getParameters().get('id')];
factureId = ApexPages.currentPage().getParameters().get('id');
pdfName = 'Facture '+f.Name;
parentId = f.Devis__c;
}
It will increase test coverage to 84%.
Please mark this as best answer if it helps.
Thanks,
Grazitti Team.
expecting right curly bracket, found 'if' at line 4
Can you give me the entire test class ?
Modify your code as below in PdfGeneratorController and leave test class as it is. You should fix other syntax errors yourself.
public with sharing class PdfGeneratorController {
public PdfGeneratorController(ApexPages.StandardController controller) {
}
public ID parentId {get;set;}
public String pdfName {get;set;}
public ID factureId {get;set;}
public PageReference savePdf() {
if (Test.IsRunningTest()){
Facture__c fact = new Facture__c();
fact.Devis__c = '0Q0L0000000AH4m';
insert fact;
PageReference pref = Page.Facture_Puerto_cacao_pdf;
pref.getParameters().put('id',fact.Devis__c);
Test.setCurrentPage(pref);
factureId = fact.Devis__c;
pdfName = 'Facture '+fact.Name;
parentId = fact.Devis__c;
}else{
Facture__c f = [Select Devis__c, Name From Facture__c Where Id = :ApexPages.currentPage().getParameters().get('id')];
factureId = ApexPages.currentPage().getParameters().get('id');
pdfName = 'Facture '+f.Name;
parentId = f.Devis__c;
}
PageReference pdf = Page.Facture_Puerto_cacao_pdf;
// add parent id to the parameters for standardcontroller
pdf.getParameters().put('id',parentId);
// create the new attachment
Attachment attach = new Attachment();
// the contents of the attachment from the pdf
Blob body;
try {
// returns the output of the page as a PDF
body = pdf.getContent();
// need to pass unit test -- current bug
} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
}
attach.Body = body;
// add the user entered name
attach.Name = pdfName;
attach.IsPrivate = false;
// attach the pdf to the account
attach.ParentId = factureId;
insert attach;
// send the user to the account to view results
return new PageReference('/'+factureId);
}
}
Thanks,
Grazitti Team.