+ Start a Discussion
anillllanillll 

HI,Can any body help in getting test coverage

public class MyAccountListCntrlr {


     // PROPERTIES
      public String subject { get; set; }
      public String body { get; set; }
      public string ccaddress{set;get;}
      public string bccaddress{set;get;}
  
     public List<ContactWrapperCls> acctList {get;set;}
     public List<String> selAccountNames{get;set;}
     public Boolean hasSelAcct {get;set;}
     public final Deal__c opp;
     public task t = new task();
     // CONSTRUCTOR
     public MyAccountListCntrlr(ApexPages.StandardController controller){
          acctList = new List<ContactWrapperCls>();
          selAccountNames= new List<String>();
          opp = [SELECT Name,id,Account__c FROM Deal__c
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
                   system.debug('opportunity@@@@@@@@@@'+opp);                  

        for(Contact a : [SELECT Name,id,Email,Account.Name,Account.Id FROM Contact where Account.id=:opp.Account__c ]){
               acctList.add(new ContactWrapperCls(a));
            Account acc=[Select Id,BillingStreet,BillingCity,Billingstate,BillingPostalcode,BillingCountry from Account where id=:opp.Account__c];
          } 

     }

     // METHODS
     public void displaySelectedAccountNumbers(){
          selAccountNames.clear();
          hasSelAcct = false;
          for(ContactWrapperCls cWrapper : acctList){
               if(cWrapper.isSelected){
                    hasSelAcct = true;
                    selAccountNames.add(cWrapper.cContact.Email);
               }
          }
     }
   
      public PageReference send() {
     
     // Define the email 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        
     // Reference the attachment page and pass in the OpportunityID
        PageReference pdf =  Page.Quotation;
        pdf.getParameters().put('id',(String)opp.id); 
        pdf.setRedirect(true);

     // Take the PDF content
            Blob b = pdf.getContent();
            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 =opp.Name +''+ 'License Version' + c +'.pdf';
            myAttach.body = b;
            insert myAttach;
           

     // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(myAttach.name);
        efa.setBody(b);

        String[] ccAddresses = new String[] {ccaddress};
        String[] bccAddresses = new String[] {'emailtosalesforce@28wj1f7u1jx4d0rk7syg098pe8grkt8ir75tm5j0vhveterlrr.z-87epmai.z.le.sandbox.salesforce.com'};
        String addresses;
     /*   if (con[0].Email != null) {
       
            address = con[0].Email;
            // Loop through the whole list of contacts and their emails 
    
            for (Integer i = 1; i <con.size(); i++) {
                if (con[i].Email != null) {
                    address += ':' + con[i].Email;
                }
            }
        }*/
      //  addresses = con.Email;
        String[] toAddresses = selAccountNames;
        
       
        
    
     // Sets the paramaters of the email 
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
            
    string UserId = UserInfo.getUserId();
    User user = [select email, IsActive from User where id = :UserId];
        

        
     //email.setCcAddresses(toAddresses);
     //email.setBccAddresses(bccaddress);
        email.setReplyTo(user.email);
        email.setBccSender(true);
        email.setHtmlBody(body);
        email.setSenderDisplayName('Salesforce');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); 
        
        // Sends the email 
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

        t.subject ='Email:'+myAttach.name+ subject;
        t.status = 'Completed';
        t.description = body;
        t.type =  'Email';
        t.ActivityDate = System.today();
        t.Whatid=opp.id;

        insert t;
            
        //PageReference pr = new PageReference('/' + ApexPages.currentPage().getParameters().get('id') +'#'+ ApexPages.currentPage().getParameters().get('id') + '_RelatedNoteList_target');
        PageReference pr = new PageReference('/'+ ApexPages.currentPage().getParameters().get('id'));
      
        pr.setRedirect(true);
      
        System.debug('Inside back');
        return pr;
        return null;
       }

public class ContactWrapperCls {
     public Boolean isSelected {get;set;}
     public Contact cContact {get;set;}

     public ContactWrapperCls(Contact cContact){
          this.cContact = cContact;
     }
}
private static testMethod void myUnitTest() {
  Account acc=new Account(Name='rose',BillingCity='bang',Billingstate='kat',BillingPostalcode='560000',BillingCountry='india');
        insert acc;
         system.debug('########555'+ acc);
        Deal__c opp=new Deal__c(Name='test12',Account__c=acc.id);
        insert opp;
        system.debug('########7765'+ opp);
     
    Contact con =  new Contact();
    con.FirstName = 'Anil';
    con.LastName = 'Dutt';
    con.Email = 'anil@swiftsetup.com';
    insert con;


    Deal__c oppNew =  new Deal__c();
    oppNew.Name = 'Test Opp';
    oppNew.Stage__c = 'Business Case';
   
    insert oppNew;


   //ApexPages.StandardController sc = new ApexPages.StandardController(con);
   //SendConfirmation sc1=new SendConfirmation (sc);
   //sc1.SendEmail();
}
}

 

 
Jeff MayJeff May

What is the problem you are running into?  You can add public method calls as needed to the testmethod and use the test results in ForceIDE to see which lines and conditions are not being covered.

Vinit_KumarVinit_Kumar

Anil,

 

Have you written something and let us know what is the issue you are running into.We can certainly take a look on that.

anillllanillll

System.QueryException: List has no rows for assignment to SObject

public class MyAccountListCntrlr {


     // PROPERTIES
      public String subject { get; set; }
      public String body { get; set; }
      public string ccaddress{set;get;}
      public string bccaddress{set;get;}
  
     public List<ContactWrapperCls> acctList {get;set;}
     public List<String> selAccountNames{get;set;}
     public Boolean hasSelAcct {get;set;}
     public final Deal__c opp;
     public task t = new task();
     // CONSTRUCTOR
     public MyAccountListCntrlr(ApexPages.StandardController controller){
          acctList = new List<ContactWrapperCls>();
          selAccountNames= new List<String>();
          opp = [SELECT Name,id,Account__c FROM Deal__c
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
                   system.debug('opportunity@@@@@@@@@@'+opp);                  

        for(Contact a : [SELECT Name,id,Email,Account.Name,Account.Id FROM Contact where Account.id=:opp.Account__c ]){
               acctList.add(new ContactWrapperCls(a));
            Account acc=[Select Id,BillingStreet,BillingCity,Billingstate,BillingPostalcode,BillingCountry from Account where id=:opp.Account__c];
           
          } 

     }

     // METHODS
     public void displaySelectedAccountNumbers(){
          selAccountNames.clear();
          hasSelAcct = false;
          for(ContactWrapperCls cWrapper : acctList){
               if(cWrapper.isSelected){
                    hasSelAcct = true;
                    selAccountNames.add(cWrapper.cContact.Email);
               }
          }
     }
   
      public PageReference send() {
     
     // Define the email 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        
     // Reference the attachment page and pass in the OpportunityID
        PageReference pdf =  Page.Quotation;
        pdf.getParameters().put('id',(String)opp.id); 
        pdf.setRedirect(true);

     // Take the PDF content
            Blob b = pdf.getContent();
            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 =opp.Name +''+ 'License Version' + c +'.pdf';
            myAttach.body = b;
            insert myAttach;
           

     // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(myAttach.name);
        efa.setBody(b);

        String[] ccAddresses = new String[] {ccaddress};
        String[] bccAddresses = new String[] {'emailtosalesforce@28wj1f7u1jx4d0rk7syg098pe8grkt8ir75tm5j0vhveterlrr.z-87epmai.z.le.sandbox.salesforce.com'};
        String addresses;
     /*   if (con[0].Email != null) {
       
            address = con[0].Email;
            // Loop through the whole list of contacts and their emails 
    
            for (Integer i = 1; i <con.size(); i++) {
                if (con[i].Email != null) {
                    address += ':' + con[i].Email;
                }
            }
        }*/
      //  addresses = con.Email;
        String[] toAddresses = selAccountNames;
        
       
        
    
     // Sets the paramaters of the email 
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
            
    string UserId = UserInfo.getUserId();
    User user = [select email, IsActive from User where id = :UserId];
        

        
     //email.setCcAddresses(toAddresses);
     //email.setBccAddresses(bccaddress);
        email.setReplyTo(user.email);
        email.setBccSender(true);
        email.setHtmlBody(body);
        email.setSenderDisplayName('Salesforce');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); 
        
        // Sends the email 
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

        t.subject ='Email:'+myAttach.name+ subject;
        t.status = 'Completed';
        t.description = body;
        t.type =  'Email';
        t.ActivityDate = System.today();
        t.Whatid=opp.id;

        insert t;
            
        //PageReference pr = new PageReference('/' + ApexPages.currentPage().getParameters().get('id') +'#'+ ApexPages.currentPage().getParameters().get('id') + '_RelatedNoteList_target');
        PageReference pr = new PageReference('/'+ ApexPages.currentPage().getParameters().get('id'));
      
        pr.setRedirect(true);
      
        System.debug('Inside back');
        return pr;
        return null;
       }

public class ContactWrapperCls {
     public Boolean isSelected {get;set;}
     public Contact cContact {get;set;}

     public ContactWrapperCls(Contact cContact){
          this.cContact = cContact;
     }
}
private static testMethod void myUnitTest() {
/*
  Account acc=new Account(Name='rose',BillingCity='bang',Billingstate='kat',BillingPostalcode='560000',BillingCountry='india');
        insert acc;
         system.debug('########555'+ acc);
        Deal__c opp=new Deal__c(Name='test12',Account__c=acc.id);
        insert opp;
        system.debug('########7765'+ opp);
     
    Contact con =  new Contact();
    con.FirstName = 'Anil';
    con.LastName = 'Dutt';
    con.Email = 'anil@swiftsetup.com';
    insert con;


    Deal__c oppNew =  new Deal__c();
    oppNew.Name = 'Test Opp';
    oppNew.Stage__c = 'Business Case';
   
    insert oppNew;
     Attachment myAttach1 = new Attachment();
        myAttach1.ParentId =opp.id;
        myAttach1.name = 'Quotation.pdf';
        myAttach1.body = blob.valueof('test');
          
        
        
        
        system.debug('Attahment1'+ myAttach1);
     
        AttachController atc = new AttachController(new ApexPages.StandardController(opp));
        system.debug('%%%%%'+atc);
        PageReference pageRef = Page.Quotation;
        pageRef.getParameters().put('id', String.valueOf(opp.Id));
        Test.setCurrentPage(pageRef);
       Blob b;
       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
     
       task t=new task();
       
       


}
}*/
    Deal__c d=new Deal__c();
       d = [select Id from Deal__c limit 1];
     PageReference ParentPage = new PageReference('/' + d.Id);  
      PageReference pdf =  Page.Quotation;
       Test.setCurrentPageReference(pdf);
        
        Test.startTest();
         ApexPages.StandardController sc = new ApexPages.standardController(d);
         MyAccountListCntrlr mySendEmailController =new MyAccountListCntrlr(sc);
          PageReference testPage1 = new PageReference('/apex/Quotation' + '?id=' + d.Id);
        ApexPages.currentPage().getParameters().put('Id',d.Id);
        //mySendEmailController.setccaddress('arup.sarkar@nb.com');
        //String get_ccaddress = mySendEmailController.getCC();
        // PageReference findContacts = mySendEmailController.findContacts();        
        //List<selectOption> items = mySendEmailController.getItems();
        //String testContacts = mySendEmailController.getSelectedContacts();
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
      Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
       // PageReference send_email_page = mySendEmailController.SendEmail();
        //System.assertEquals(send_email_page.getURL(), ParentPage.getURL());
        
        
        Test.stopTest();       
       }}

 

Vinit_KumarVinit_Kumar

You are getting List has no rows for assignment to SObject on which line.Can you post the exact error message.

anillllanillll

As you can see in code at first i want to get coverage on the mentioned below

 

 public String subject { get; set; }
      public String body { get; set; }
      public string ccaddress{set;get;}
      public string bccaddress{set;get;}
  
     public List<ContactWrapperCls> acctList {get;set;}
     public List<String> selAccountNames{get;set;}
     public Boolean hasSelAcct {get;set;}

 

Please help me on this.

 

As of now im not getting any test coverage. please help me how to start.

Vinit_KumarVinit_Kumar

To crate a test class for Wrapper class you need to create a boolean property which will be true only when we are executing test class. If that property is true then set isSelected property of your wrapper class as true.

 

 

anillllanillll

i just want to cover insert myattach;   in test method 

anillllanillll
            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 =opp.Name +''+ 'License Version' + c+'.pdf';
            myAttach.body = b;
            insert myAttach;
           

 can u pls help in covering this one