You need to sign in to do that
Don't have an account?
Tatiana 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>
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>
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
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.
Update your test mehod with this code :-
Hope this helps,
--
Thanks
Swayam
Thank yous so much for your help.
I think I am getting an error regarding person account.
Error:
We have person accounts in our org, would the code be different? This is what I have.
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
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