+ Start a Discussion
Lavanya Ponniah 3Lavanya Ponniah 3 

How to write test class for the below code?

public class sendquoteemail{

    public String getBcc() {
        return null;

  public String emailSubject { get; set; }

 public quote getQ() {
        return q;

    public sendquoteemail(ApexPages.StandardController controller) {}
    public String emailVal{get;set;}
    public String cc{get;set;}
    public String subject{ get; set; }
    public String body { get; set; }
    public Account accname{get;set;}  
    Public string aname; 
    private List<String> ccEmailIds;
    public  quote q;
    public Attachment att{get;set;}
    public List<Attachment> attList{get;set;}
    public sendquoteemail()
        q= [SELECT email,contactid,cc__c,sbject__c,body__c,accountid,id from quote WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        accname=[select name from Account where id=:q.accountid];
        //body="abc" & BR() & "xyz";
        attList = new List<Attachment>();
        att = new Attachment();
    public void upload()
        if(att.name == null)
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please Select File Path First.'));
            att.OwnerId = UserInfo.getUserId();

            att = new Attachment();
    public PageReference send()
         List<Messaging.EmailFileAttachment> mailAttList = new List<Messaging.EmailFileAttachment>();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        PageReference pdf =  Page.ProageQuoteTemplate;
        Blob b = pdf.getContent();
         Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        QuoteDocument doc = new QuoteDocument(Document = b, QuoteId = q.id);
        insert doc;

        for(Attachment att1 : attList)
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        string[] ToAddresses=new string[] {q.email};
        email.setSubject( q.sbject__c );
        email.setPlainTextBody( q.body__c );
        if( q.cc__c !='' && q.cc__c.length() > 0 )
            ccEmailIds = q.cc__c.split(';');
        if(ccEmailIds != null && ccEmailIds.size()>0)
            ccEmailIds = q.cc__c.split(';');
        //email.setFileAttachments(new Messaging.EmailFileAttachment[] {mailAttList});      
        Messaging.SendEmailResult[] sendResult;

          Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }); 
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '));
        PageReference pageRef = new ApexPages.StandardController(q).view();
        return pageRef;
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test. 

Each test should follow the following structure:
  1. Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  2. Starting the test. This is calling Test.startTest() to reset the governor limits
  3. Calling your class / method
  4. Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  5. Asserting that your changes have worked
    1. If you have inserted/updated/deleted data, you need to query for the updates
    2. Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/