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
Tatiana Cooke 9Tatiana Cooke 9 

Test Class not touching my Code

Team, 

Pretty new to the development side of things and need help on the following class. It a class with a visualforce page (Button) that adds attachements to the notes and attachements section and refreshes to the parent page when ckick save. 

For some reason the test class is not running my class and achieving any code coverage. 

Appreciate any help. 

Below is the test class: 
public class attachmentsample {

    public attachmentsample(ApexPages.StandardController controller) {

    }
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }
   
    Public Pagereference Save()
    {
        String accid = System.currentPagereference().getParameters().get('id');

        Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
         
         /* insert the attachment */
         insert a;
        return NULL;
    }   

}

Test class:
@isTest
public class Test2attachmentsample {
  private static testMethod void attachmentsampletest() {


    Tenant_Coordination__C TC = new Tenant_Coordination__C(Name ='Test');
    insert TC; 
    
     Attachment attach=new Attachment();    
     attach.Name='Unit Test Attachment';
     Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
     attach.body=bodyBlob;
     attach.parentId=TC.id;
     insert attach;

     List<Attachment> attachments=[select id, name from Attachment where parent.id=: TC.Id];
     System.assertEquals(1, attachments.size());
    }
}

 
Best Answer chosen by Tatiana Cooke 9
Jose Mario RodriguezJose Mario Rodriguez
You need to add the standarController variable and assign it to the parametrized contructor, I give the example :)
 
@isTest
public class Test2attachmentsample {
  private static testMethod void attachmentsampletest() {
  	/*Custom Object*/
    Tenant_Coordination__C TC = new Tenant_Coordination__C(Name ='Test');
    insert TC;

    /*set Id to url parameters */
    ApexPages.currentPage().getParameters().put('id', TC.Id);
    /*initial standard controller variable*/
    ApexPages.StandardController sc = new ApexPages.standardController(TC);

    /*class instance*/
    attachmentsample att_ctr = new attachmentsample(sc);

    Attachment attach=new Attachment();    
    attach.Name='Unit Test Attachment';
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    attach.body=bodyBlob;

    /*set values to attachment on the controller [attachmentsample]*/
    att_ctr.myfile = attach;

    /*call all methods of controller [attachmentsample]*/
    att_ctr.Save();
    att_ctr.getmyfile();
    /*
     
     attach.parentId=TC.id;
     insert attach;
	*/
     List<Attachment> attachments=[select id, name from Attachment where parent.id=: TC.Id];
	
		
     System.assertEquals(1, attachments.size());
     
    }
}

 

All Answers

Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

In test class you have pass parameters to the visualforce page

@isTest
public class Test2attachmentsample {
  private static testMethod void attachmentsampletest() {


    Tenant_Coordination__C TC = new Tenant_Coordination__C(Name ='Test');
    insert TC; 
    
     PageReference ref = Page.yourpagename;
     Test.setCurrentPage(ref);
     ApexPages.currentPage().getParameters().put('id, TC.id);
     Attachment attach=new Attachment();    
     attach.Name='Unit Test Attachment';
     Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
     attach.body=bodyBlob;
     attach.parentId=TC.id;
     insert attach;

     List<Attachment> attachments=[select id, name from Attachment where parent.id=: TC.Id];
     System.assertEquals(1, attachments.size());
    }
}
 


 

Tatiana Cooke 9Tatiana Cooke 9
Thank you! 

Getting the following error when entering your code. Error: Compile Error: line breaks not allowed in string literals at line 8 column -1. 

Any thoughts?

Regards, 

TC

 
Tatiana Cooke 9Tatiana Cooke 9
Team, 

I  was able to fix the above issue by changingApexPages.currentPage().getParameters().put('id, TC.id); to   ApexPages.currentPage().getParameters().put('id', TC.id);
 
However I am still not getting any code coverage.
 
I noticed in the above code I used   PageReference ref = Page.yourpagename; which in reality my visualforce page should be "Attach Invoice" 

Even after updating both the above I still have Code Coverage 0% (0/9) on my attachment sample class. 

Any thoughts?
Jose Mario RodriguezJose Mario Rodriguez
You need to add the standarController variable and assign it to the parametrized contructor, I give the example :)
 
@isTest
public class Test2attachmentsample {
  private static testMethod void attachmentsampletest() {
  	/*Custom Object*/
    Tenant_Coordination__C TC = new Tenant_Coordination__C(Name ='Test');
    insert TC;

    /*set Id to url parameters */
    ApexPages.currentPage().getParameters().put('id', TC.Id);
    /*initial standard controller variable*/
    ApexPages.StandardController sc = new ApexPages.standardController(TC);

    /*class instance*/
    attachmentsample att_ctr = new attachmentsample(sc);

    Attachment attach=new Attachment();    
    attach.Name='Unit Test Attachment';
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
    attach.body=bodyBlob;

    /*set values to attachment on the controller [attachmentsample]*/
    att_ctr.myfile = attach;

    /*call all methods of controller [attachmentsample]*/
    att_ctr.Save();
    att_ctr.getmyfile();
    /*
     
     attach.parentId=TC.id;
     insert attach;
	*/
     List<Attachment> attachments=[select id, name from Attachment where parent.id=: TC.Id];
	
		
     System.assertEquals(1, attachments.size());
     
    }
}

 
This was selected as the best answer
Tatiana Cooke 9Tatiana Cooke 9
You guys are amazing!!! 

Thank you!! #happygal
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Sorry you have taken standard controller I haven't seen that. However you got the soultion cheers :) :D 
Jose Mario RodriguezJose Mario Rodriguez
Yeah, well the controller can be programmed using best practices, I mean using only what is required, I see that it is not necesary to use standardController in the constructor and perhaps use variable attachment with a variable apex properties maybe this example is more eficient.

I do not know the initial requirement for functionality with visualforce.

 
public with sharing class uploadAttachment_ctr {
    private String parentId;
    public Attachment attach {get; set;}
   
    public uploadAttachment_ctr(){
        this.parentId = ApexPages.currentPage().getParameters().get('id');
        this.attach = new Attachment();
    }

    public PageReference save(){
        this.attach.ParentId = parentId;
        insert attach;
        return null;
    } 
}



 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Yeh Jose, I agree with you instead of over riding button with visualforce page we can create a URL button and send id with the URL (So we can get rid of StandardController) :) ;)