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
An_jaAn_ja 

Class with Attachment - cannot get past 64% test coverage (attachment not covered)

public class saveAndThanks 
{
                
        
      public Sponsorship_Request__c SpR{get;set;}
      private ApexPages.StandardController controller;
      public Blob upload {get; set;}
      public String contentType {get; set;}
      public String fileName {get; set;}
      public saveAndThanks(ApexPages.StandardController controller) 
        
        {
                //this.controller = controller;
                SpR = new Sponsorship_Request__c();
                this.SPR=(Sponsorship_Request__c)controller.getRecord();
                
        }
        
        
      public PageReference saveAndThanks2() 
        {        
                //OwnerId = '00GE0000000hCBH',
                SpR.RecordTypeId = '01250000000DvDq';
                SpR.Account__c = '0015000000ch107';
                
           try
           {
                insert SpR;
                system.debug('######'+SpR);
           }
        
           catch(DmlException ex)
           {
                ApexPages.addMessages(ex);
           }
        
           if(upload!=null)
           {
                Attachment attach=new Attachment();
                attach.Body=upload;
                attach.Name=filename;
                attach.ContentType=contentType;
                attach.ParentID=SPR.id;
    
             insert(attach);
              
       
          }
       
        PageReference thanksPage = new PageReference('/apex/saveAndThanks');
        thanksPage.setRedirect(true);
        return thanksPage;
      }
        
        
      public void saveAndThanks()
        {
        
        }
        
      public PageReference cancel()
        {
           PageReference refreshPage = new PageReference('/apex/Cancelled');
           refreshPage.setRedirect(true);
           return refreshPage;
        }
        
        
    static testMethod void testsaveAndThanks() 
    {

       
        Sponsorship_Request__c SpR = new Sponsorship_Request__c();
        
                
        SpR.Organization__c = 'Test Job';
        SpR.Account__c = '0015000000ch107';
        SpR.Contact_Name__c = 'test spr name';
        SpR.Event_Project_Name__c = 'test event';
        SpR.Request_Amount__c = 5;
        SpR.Potential_Referral_Source__c = 'No';
        SpR.Options__c = 'test project name';
        insert SpR;
        
        saveAndThanks controller = new saveAndThanks(new ApexPages.StandardController(SpR));
        
        
        Attachment attach= new Attachment();   
        attach.Name = 'Contact Picture';
        Blob bodyBlob = Blob.valueOf('upload');
        attach.body = bodyBlob;
        attach.ParentId = SpR.id;  
        
        insert attach;
               
        
        controller.saveAndThanks2();
        
        
        System.debug('the current page is...' +ApexPages.currentPage() );

        System.assertEquals('test spr name', SpR.Contact_Name__c);
        
        System.assertEquals('Contact Picture', attach.Name);
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:SpR.id];
        
        System.assertEquals(1, attachments.size());

        }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

So well seems like 

Your code expects upload != NULL to insert a attachment

if(upload!=null)

But in the testclass you just inserted the attachment without even using the original class


Attachment attach= new Attachment(); attach.Name = 'Contact Picture'; Blob bodyBlob = Blob.valueOf('upload'); attach.body = bodyBlob; attach.ParentId = SpR.id; insert attach;

 

I guess you need to replace the above piece by 

 

        controller.FileName = 'Contact Picture';
        controller.upload = Blob.valueOf('upload');
        controller.SpR = SpR;  

 

All Answers

Avidev9Avidev9

So well seems like 

Your code expects upload != NULL to insert a attachment

if(upload!=null)

But in the testclass you just inserted the attachment without even using the original class


Attachment attach= new Attachment(); attach.Name = 'Contact Picture'; Blob bodyBlob = Blob.valueOf('upload'); attach.body = bodyBlob; attach.ParentId = SpR.id; insert attach;

 

I guess you need to replace the above piece by 

 

        controller.FileName = 'Contact Picture';
        controller.upload = Blob.valueOf('upload');
        controller.SpR = SpR;  

 

This was selected as the best answer
An_jaAn_ja

82% Merci beaucoup! 

 

static testMethod void testsaveAndThanks() 
    {

       
        Sponsorship_Request__c SpR = new Sponsorship_Request__c();
        
                
        SpR.Organization__c = 'Test Job';
        SpR.Account__c = '0015000000ch107';
        SpR.Contact_Name__c = 'test spr name';
        SpR.Event_Project_Name__c = 'test event';
        SpR.Request_Amount__c = 5;
        SpR.Potential_Referral_Source__c = 'No';
        SpR.Options__c = 'test project name';
        insert SpR;
        
        saveAndThanks controller = new saveAndThanks(new ApexPages.StandardController(SpR));
        
        
        Attachment attach= new Attachment();   
        
        controller.FileName = 'Contact Picture';
        controller.upload = Blob.valueOf('upload');
        controller.SpR = SpR; 
        
        
        controller.saveAndThanks2();
        
        
        System.debug('the current page is...' +ApexPages.currentPage() );

        System.assertEquals('test spr name', SpR.Contact_Name__c);
        
        

        }
}