+ 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')];
        system.debug('============================================='+q.email);
        accname=[select name from Account where id=:q.accountid];
        aname=accname.name;
        //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.'));
        }
        else
        {
            att.OwnerId = UserInfo.getUserId();
            attList.add(att);

            att = new Attachment();
                        att.body=null;
        }
    }
   
    public PageReference send()
    {
         List<Messaging.EmailFileAttachment> mailAttList = new List<Messaging.EmailFileAttachment>();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       
        PageReference pdf =  Page.ProageQuoteTemplate;
        pdf.getParameters().put('id',(String)ApexPages.currentPage().getParameters().get('id'));
        pdf.setRedirect(true);
       
        Blob b = pdf.getContent();
         Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName('attachment.pdf');
        efa1.setBody(b);
        mailAttList.add(efa1);  
       
       
        QuoteDocument doc = new QuoteDocument(Document = b, QuoteId = q.id);
        insert doc;
     

       
        for(Attachment att1 : attList)
        {
            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
            efa.setBody(att1.body);
            efa.setFileName(att1.name);
            mailAttList.add(efa);
        }
       
        string[] ToAddresses=new string[] {q.email};
        email.setSubject( q.sbject__c );
        email.setToAddresses(ToAddresses);
        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.setCcAddresses(ccEmailIds);
        }
        //email.setFileAttachments(new Messaging.EmailFileAttachment[] {mailAttList});      
          email.setFileAttachments(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;
    }  
 } 
pconpcon
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/