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
anillllanillll 

Urgent: Please help me in test method to get coverage

Apex class

public with sharing class AttachController {

//private ApexPages.StandardController controller { get; set; }
public List<Deal_License__c> dp{get;set;}

   public Deal__c D{get;set;}
    public AttachController(ApexPages.StandardController controller) {
     D = [Select Name,Id,Account__c,Account__r.Name,Rate__c,Total_Value__c,Account__r.BillingStreet,
                 Account__r.BillingCity,Account__r.Billingstate,Account__r.BillingPostalcode,Account__r.BillingCountry
                 from Deal__c where id=:ApexPages.currentPage().getParameters().get('id')];
    system.debug('TestDeal'+D);
    Account acc=[Select Id,BillingStreet,BillingCity,Billingstate,BillingPostalcode,BillingCountry from Account where id=:D.Account__c];
    system.debug('Account%%%%%%%%%%%%%%'+acc);
    dp=[select id,Name,Deal__c,Months__c,List_Price__c,Unit_Price__c,Quantity__c,Total_Value__c from Deal_License__c where Deal__c=:D.Id];
    System.debug('DEALPRDODUCT&&&&&&&&&&'+dp);
    }
    
    public void attach() {
        try{
            Attachment myAttach = new Attachment();
            myAttach.ParentId = ApexPages.currentPage().getParameters().get('id');
            System.debug('Id: ' + ApexPages.currentPage().getParameters().get('id'));
            List<Attachment> at=[Select id from Attachment where parentId=:myAttach.ParentId];
            Integer c=at.size()+1;
            myAttach.name =D.Account__r.Name +''+ 'License Version' + c +'.pdf';
            PageReference psPdf = new PageReference('/apex/Quotation?id=' + ApexPages.currentPage().getParameters().get('id'));
            myAttach.body = psPdf.getContentAsPdf();

            insert myAttach;
              
            
                       
            System.debug('After attach');
            System.debug(myAttach);

            
            /*
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Quotation has been attached.'));
            */
        }
        catch(Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'There was an error while attaching'));
            System.debug('Exception');
        }
    }
    
    public PageReference QA() {
     PageReference secondPage = new PageReference('/apex/EmailQuote?id=' + ApexPages.currentPage().getParameters().get('id'));
      Attachment myAttach = new Attachment();
            myAttach.ParentId = ApexPages.currentPage().getParameters().get('id');
            System.debug('Id: ' + ApexPages.currentPage().getParameters().get('id'));
            List<Attachment> at=[Select id from Attachment where parentId=:myAttach.ParentId];
            Integer c=at.size()+1;
            myAttach.name = D.Account__r.Name +''+ 'License Version' + c +'.pdf';
            PageReference psPdf = new PageReference('/apex/Quotation?id=' + ApexPages.currentPage().getParameters().get('id'));
            myAttach.body = psPdf.getContentAsPdf();

            insert myAttach;
              
            secondPage.setRedirect(true);   
            return secondPage; 
    }
    public PageReference back() {
        attach();
        PageReference pr = new PageReference('/' + ApexPages.currentPage().getParameters().get('id') +'#'+ ApexPages.currentPage().getParameters().get('id') + '_RelatedNoteList_target');
        pr.setRedirect(true);
        System.debug('Inside back');
        return pr;
    }
       public PageReference cancel() {
       PageReference pr = new PageReference('/' + ApexPages.currentPage().getParameters().get('id') +'#'+ ApexPages.currentPage().getParameters().get('id') + '_RelatedNoteList_target');
       pr.setRedirect(true);
       System.debug('Inside back');
       return pr;
       }
        /*test methods here*/
     public static testMethod void testAttachController()
      {    
        //Opportunity opp=[select id from Opportunity where Name=:'Prasad Punage-'];
        Account acc=new Account(Name='rose',BillingCity='bang',Billingstate='kat',BillingPostalcode='560000',BillingCountry='india' );
        insert acc;
        Deal__c opp=new Deal__c(Name='test12',Account__c='acc.id');
        insert opp;
       
        Deal_License__c dl=new Deal_License__c(Name='asd',Deal__c='opp.id');
        insert dl;        
        Attachment myAttach1 = new Attachment();
        myAttach1.ParentId =opp.id;
        myAttach1.name = 'Quotation.pdf';
        myAttach1.body = blob.valueof('test');
          
        insert myAttach1;
        system.debug('Attahment1'+ myAttach1);
        AttachController atc = new AttachController(new ApexPages.StandardController(myAttach1));
         
        atc.attach();
        atc.back();
        atc.cancel();
      }
      
      public String attachmentName{get; set;}
      public pagereference AttachQuote(){
        Pagereference pdf = page.Quotation;
        blob  b = pdf.getContent();
    
         attachment att = new attachment();
         att.parentId = ApexPages.currentPage().getParameters().get('id') ;
         att.body = b;
         att.name = 'Quotation.pdf';     
         insert att;
         
         return null;
      
      }
}

 Please help me in test class....

 

Thanks in Advance............