function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

need test class for apex class

iam new to apex.urgent need of test class for apex class


my apex class


public class MyController {
public ID accountId {get;set;}
  public String email {get;set;}
public String getName() {
return 'MyController';
public Opportunity getOpportunity() {
return [select Account.Id,, Account.G_English_Name__c, Account.B_English_Name__c,, Contact__r.MailingStreet,Contact__r.MailingCity,
        Contact__r.MailingState, Contact__r.MailingPostalCode, Contact__r.MailingCountry from Opportunity
where id = :ApexPages.currentPage().getParameters().get('id')];

public PageReference sendPdf() {

    PageReference pdf = Page.pdfgeneratortemplate;
    // add parent id to the parameters for standardcontroller
    // 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');
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.Body = body;
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new String[] { '' });
    mail.setSubject('PDF Email Demo');
    mail.setHtmlBody('Here is the email you requested! Check the attachment!');
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
    // Send the email
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+email));
    return null;


Have you tried writing the test class at all? It's the best way to learn.




I agree with Wes, test classes are not as hard as they seem. Also, writing test classes is just as much a part of development as writing the production code itself.


Start with the beginnings of your first testmethod, and think about how you would recreate one of your test cases with Apex. In your case, you need to

  • find an Account to use for testing,
  • set a Visualforce page as your currentpage,
  • give it your account ID as a parameter,
  • start the controller and run your method, then
  • make sure it ran the way you expected (using asserts)

Good luck!