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
Lavanya Ponniah 3Lavanya Ponniah 3 

How to write test class for the below code.

public class sendEmail5{ 

    public quote getQ() {
        return q;
    }

    public sendEmail5(ApexPages.StandardController controller) {}
    public String emailVal{get;set;}
    public String cc{get;set;}
    public String subject{ get; set; }
    public String body { get; set; }
 
    private List<String> ccEmailIds;
    public  quote q;

  
    public sendEmail5() {
        q= [SELECT email,contactid,cc__c,sbject__c from quote WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        system.debug('============================================='+q.email);
       

    }

    public PageReference send() {
       
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       
        PageReference pdf =  Page.QuoteTemplate;
        pdf.getParameters().put('id',(String)ApexPages.currentPage().getParameters().get('id'));
        pdf.setRedirect(true);
       
        //Blob b = pdf.getContent();
        Blob b = !Test.isRunningTest() ? pdf.getContent() : Blob.valueOf('This is the test');

       
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('attachment.pdf');
        efa.setBody(b);       
       
        string[] ToAddresses=new string[] {q.email};
        email.setSubject( q.sbject__c );
        email.setToAddresses(ToAddresses);
        email.setPlainTextBody( body );
        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[] {efa});      
         
          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;
    }
}
Vatsal KothariVatsal Kothari
Hi Lavanya,

You can refer below links for test class:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

http://www.sundoginteractive.com/sunblog/posts/testing-controllers-in-salesforce

http://blog.jeffdouglas.com/2010/06/02/testing-salesforce-com-controller-extensions/

If this solves your problem, kindly mark it as the best answer.

Thnaks,
Vatsal

Lavanya Ponniah 3Lavanya Ponniah 3
Now this is a test class for that controller it covers only 64% and shows null pointer exception of Class.sendEmail5.send: line 44, column 1
Class.TestsendEmail5.testDoSomething: line 33, column 1


@isTest
private class TestsendEmail5
{
    static testMethod void testDoSomething()
    {
       Opportunity  o = new Opportunity( name = 'opportunity',CloseDate = System.today()+10,StageName='Closed Won');
       insert o ;
       
       Quote q1=new Quote(name='test', OpportunityId = o.Id, sbject__c='Test', email='kala.lavs@gmail.com');
       
        System.debug('Email before inserting new Quote: ' + q1.email);
        insert q1;
       
        Quote q2 = [SELECT name, email,contactid,cc__c,sbject__c from quote WHERE name =:'test'];
        System.assertEquals(q1.Id, q2.Id);
        
                 
        PageReference pageRef = new PageReference('/apex/QuoteTemplate?id=' + q1.Id);

       //  PageReference pageRef = Page.QuoteTemplate;
      //  pageRef.getParameters().put('id',q1.Id);
         Test.setCurrentPageReference(pageRef);
         Test.startTest();
        string ID = ApexPages.currentPage().getParameters().get('id');
        System.assertEquals(q1.id, ID);
       
        sendEmail5 myPageCon = new sendEmail5();
       
        myPageCon.cc = 'lavanya@ideallsf.com';
     myPageCon.subject = q1.Sbject__c;              
        myPageCon.body = 'This is Sample';
       
        pageRef = myPageCon.send();
        System.debug('the current page is...' +ApexPages.currentPage() );
       
        System.assertEquals('lavanya@ideallsf.com', q1.email); 
       
        List<Attachment> attachments=[select id, name from Attachment where parentid=:q1.id];       
        Test.stopTest();        
    }
}
Vatsal KothariVatsal Kothari
You have not assign any value to "cc__c" field in q1 object.
Lavanya Ponniah 3Lavanya Ponniah 3
Its working now.