+ Start a Discussion
laurent.greiwelding1.3974669707216677E12laurent.greiwelding1.3974669707216677E12 

Can any one help me writing test class for a class ?

I have this class :
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 
Best Answer chosen by laurent.greiwelding1.3974669707216677E12
Grazitti TeamGrazitti Team
Hi Laurent,

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

SonamSonam (Salesforce Developers) 
Please run this test in the developer console and check for the lines which are still not covered - once you get the lines -you can then write specific test methods .

Can you paste those lines here? 
laurent.greiwelding1.3974669707216677E12laurent.greiwelding1.3974669707216677E12
This lines are not covered. Can you help me ?
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);
laurent.greiwelding1.3974669707216677E12laurent.greiwelding1.3974669707216677E12
Bonjour, Je suis absent jusqu'au 31 octobre 2014. Je traiterai votre demande à mon retour le 3 novembre. En cas d'urgence, vous pouvez contacter le Service Informatique au 03 87 22 21 80.
Grazitti TeamGrazitti Team
Hi Laurent,

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.
 
laurent.greiwelding1.3974669707216677E12laurent.greiwelding1.3974669707216677E12
Thanks for your help but i have on error :

expecting right curly bracket, found 'if' at line 4

Can you give me the entire test class ?
Grazitti TeamGrazitti Team
Hi Laurent,

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.
This was selected as the best answer
laurent.greiwelding1.3974669707216677E12laurent.greiwelding1.3974669707216677E12
Bonjour, Je suis absent jusqu'au 31 octobre 2014. Je traiterai votre demande à mon retour le 3 novembre. En cas d'urgence, vous pouvez contacter le Service Informatique au 03 87 22 21 80.
laurent.greiwelding1.3974669707216677E12laurent.greiwelding1.3974669707216677E12
You are the best Grazitti Team !!!