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 B CookeTatiana B Cooke 

How to create a test class for this apex class?

Team, 

New to the developer side of Salesforce. Need to know what the test class would be for the below Apex Class and Visualforce Page. Appreciate any help or guidance. When I tried pushing to production it tested ALL previous code and would not let mine thru. Any pointers there would help as well. 

This code involves having a custom attach button within a page layout on a standard object. 

Apex 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;
    }   

}

Visualforce Page

<apex:page standardController="Tenant_Coordination__c" extensions="attachmentsample">
    <apex:form >
    <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
       <apex:commandbutton value="Save" action="{!Save}" onclick="window.top.location='/{!Tenant_Coordination__c.id}'; return true"/>
    </apex:form>
</apex:page>
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

Ex:-to check the above apex program is performing correctly or not in runtime ,you will write test method by insert in some dummy data and checking the out put in the same class by using system assert function.

 apex test class syntax:-

@isTest

private class add {

static testMethod void add() {

          Your test code...............

   }


Check this link for writing apex class and test classes:-
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm


Hope this helps,

--
Thanks,
Swayam Chouksey
@salesforceguy
Tatiana B CookeTatiana B Cooke
Team, 

So far I have the following. Again, I am new at this. I am getting the following Error: System.NullPointerException: Attempt to de-reference a null object

@isTest
private class add {

static testMethod void add() {

    {
        Tenant_Coordination__c TC=new Tenant_Coordination__c(Name='test');

        attachmentsample controller=new attachmentsample(new ApexPages.StandardController(TC));
 
        controller.myfile.Name='Unit Test Attachment';
        controller.myfile.body=Blob.valueOf('Unit Test Attachment Body');
        controller.Save();
        
        List<Attachment> attachments=[select id, name from Attachment where Name=:TC.id];
        System.assertEquals(1, attachments.size());
    } 
}}

Appreciate any help. 
 
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

Update your test mehod with this code :-
 
PageReference pageRef = Page.NameOFVF;       
 Test.setCurrentPage(pageRef);
 
 //Create Account
 Account acc = new Account(Name="Test");
 insert acc;
 
 //setting the URL parameter 
 ApexPages.currentPage().getParameters().put('id', acc.id);
 Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(acc);
  
 attachmentsample controller=new attachmentsample(sc);
 controller.myfile.Name='Unit Test Attachment';
 controller.myfile.body=Blob.valueOf('Unit Test Attachment Body');
 controller.Save();

Hope this helps,

--
Thanks
Swayam
Tatiana B CookeTatiana B Cooke
Swayam, 

Thank yous so much for your help. 

I think I am getting an error regarding person account.

Error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WE_PersonAccount_Trigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 0 Trigger.WE_PersonAccount_Trigger: line 7, column 1: []

We have person accounts in our org, would the code be different? This is what I have. 
@isTest
private class Testattachmentsample {
static testMethod void testAttachments()
{
PageReference pageRef = Page.Attach_Invoice;      
 Test.setCurrentPage(pageRef);
  
 //Create Account
Account acc=new Account(Name='Acme Inc');
insert acc;
  
 //setting the URL parameter
 ApexPages.currentPage().getParameters().put('id', acc.id);
 Apexpages.Standardcontroller sc = new Apexpages.Standardcontroller(acc);
   
 attachmentsample controller=new attachmentsample(sc);
 controller.myfile.Name='Unit Test Attachment';
 controller.myfile.body=Blob.valueOf('Unit Test Attachment Body');
 controller.Save();
 }
 }

 
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

Can you check the required field while creating the account. you need to provide all required value while creatin account in test class.

--
Thanks,
Swayam
Tatiana B CookeTatiana B Cooke
Swayam, 

We have many different record types. For this record type RecordType=012f00000000WCq it is only Account name. 

How would I call this out in the code? 

Regards,

Tatiana