You need to sign in to do that
Don't have an account?

Help With Testing Standard Controller Extension
Hello,
I have created my first Standard Controller Extention for Case standard controller to include attachments on a VF Page for case submissions.
I have not created a test class to test the controller extension before, and after looking at example after example I am even more confused now.
Can someone please help me write a test class for the following Controller extension class so that I can move this into my production environment.
Any help will be greatly appreciated, as I am very new to apex development and especially writing test classes.
Controller Extension Code:
public with sharing class sfdevcaseattachment
{
public case objcase{get;set;}
public String subj {get;set;}
public String description {get;set;}
public String Initiating_Requestor {get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;}
public Blob fileBody{get;set;}
public sfdevcaseattachment(Apexpages.standardcontroller controller)
{
objcase = new case();
myAttachment =new Attachment();
}
public pagereference save()
{
insert objcase;
if (fileName != null && fileName.trim().length() > 0 && fileBody != null) {
myAttachment = new Attachment();
Integer i=0;
myAttachment .clear();
myAttachment.Body = fileBody;
myAttachment.Name = this.fileName;
myAttachment.ParentId = objcase.id;
insert myAttachment;
}
pagereference pr = new pagereference('/'+objcase.id);
return pr;
}
}
Thank you again in advance for your help.....I have spent 3 days now studying on how to do this and I am more of a visual learner so I am completely lost.
Shawn
I have created my first Standard Controller Extention for Case standard controller to include attachments on a VF Page for case submissions.
I have not created a test class to test the controller extension before, and after looking at example after example I am even more confused now.
Can someone please help me write a test class for the following Controller extension class so that I can move this into my production environment.
Any help will be greatly appreciated, as I am very new to apex development and especially writing test classes.
Controller Extension Code:
public with sharing class sfdevcaseattachment
{
public case objcase{get;set;}
public String subj {get;set;}
public String description {get;set;}
public String Initiating_Requestor {get;set;}
public Attachment myAttachment{get;set;}
public string fileName{get;set;}
public Blob fileBody{get;set;}
public sfdevcaseattachment(Apexpages.standardcontroller controller)
{
objcase = new case();
myAttachment =new Attachment();
}
public pagereference save()
{
insert objcase;
if (fileName != null && fileName.trim().length() > 0 && fileBody != null) {
myAttachment = new Attachment();
Integer i=0;
myAttachment .clear();
myAttachment.Body = fileBody;
myAttachment.Name = this.fileName;
myAttachment.ParentId = objcase.id;
insert myAttachment;
}
pagereference pr = new pagereference('/'+objcase.id);
return pr;
}
}
Thank you again in advance for your help.....I have spent 3 days now studying on how to do this and I am more of a visual learner so I am completely lost.
Shawn
@isTest
private class AttachClassTest {
//test method
static testMethod void testCase() {
//mock up your test case record here
Case c = New Case (
Subject = 'Test Subject',
Origin = 'PHONE'
);
//reference your extensions
sfdevcaseattachment ext = new sfdevcaseattachment(new Apexpages.Standardcontroller(c));
//now set your test values for your attachments
ext.fileBody = Blob.valueOf( 'this is an attachment test' );
ext.fileName = 'TEST NAME';
//now execute the pagereference by calling the save method
ext.save();
}
}
Hope this make sense. Just remember test methods are almost a mock of what a user would do in the system.
All Answers
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008wjJIAQ
And did you read this full guide: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
If you did and still not helping, i'll help you out on this and send you a snippet to test your code.
Yes I read the full guide, and unfortunately I am more of a visual learner, so reading the guide through twice in the last 3 days still did not get me any closer to understanding this.
I also just looked at teh forum example you graciously provided, and it seems liek that woudl work, but I am not seeing the work insert so I am not sure where to add the attachment information in taht example code? Sorry, I am very very new to this and am trying my best to learn.
Could you please write up a test class for me for my solution presented so I can look at it using the nformation form my org and maybe that will help me understand it better.
Thank you my friend, I really appreciate all of your help!
Here is my VF Page code...
<apex:page standardcontroller="Case" extensions="sfdevcaseattachment"
showHeader="false">
<img src="{!$resource.AVISPL_Logo2}"></img><b/><b/>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Hello, Thank You For Your Request. Your Request Will Be Submitted For Approval. Once Your Request Has Been Approved Or Rejected You Will Be Notified." columns="1" showHeader="True" collapsible="False">
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageMessages />
<apex:pageBlock >
<apex:inputField value="{!Case.CaseNumber}" />
<apex:pageBlockSection title="Requestor Information" columns="2" showHeader="True" collapsible="False">
<apex:inputField value="{!objcase.Initiating_Requestor__c}" required="True"/>
<apex:inputField value="{!objcase.Job_Title_Position_In_Company__c}" required="True"/>
<apex:inputfield value="{!objcase.Requestor_Email_Address__c}" required="True"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Impact" columns="2" showHeader="True" collapsible="False">
<apex:inputField value="{!objcase.Priority}"/>
<apex:inputField value="{!objcase.Number_of_Users_Affected__c}" required="True"/>
<apex:inputfield value="{!objcase.Area_s_Affected__c}" required="True"/>
<apex:inputField value="{!objcase.Functionality_Affected__c}" required="True"/>
<apex:inputField value="{!objcase.Profile_s_Affected__c}" required="True"/>
<apex:inputField value="{!objcase.List_All_U_A_T_Participants__c}" required="True"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Tell Us About Your Request" columns="2" showHeader="True" collapsible="False">
<apex:inputField value="{!objcase.Description}" required="True"/>
<apex:inputField value="{!objcase.Subject}" required="True"/>
<apex:inputField value="{!objcase.Project_Name__c}" required="True"/>
<apex:inputfield value="{!objcase.Request_Type__c}" required="True"/>
<apex:inputfield value="{!objcase.Reason_For_Change__c}" required="True"/>
<apex:inputfield value="{!objcase.Benefit_Of_Change__c}" required="True"/>
<apex:inputfield value="{!objcase.Driver__c}" required="True"/>
<apex:inputfield value="{!objcase.Target_Deadline__c}" required="True"/>
<apex:inputfield value="{!objcase.Sponsoring_Department_s__c}" required="True"/>
<apex:inputfield value="{!objcase.Sponsoring_Dept_GL_Code__c}" required="True"/>
<apex:inputfield value="{!objcase.Stakeholder__c}" required="True"/>
<apex:inputField value="{!objcase.Executive_Sponsor__c}" required="True"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Attachment Or ScreenShot" columns="2" showHeader="True" collapsible="False">
<apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>
</apex:pageBlockSection>
<apex:commandButton value="Submit Request" action="{!Save}" />
<apex:outputLink value="https://svmx9-avi-spl.cs15.force.com/Main">Return To Main Page</apex:outputLink>
</apex:pageBlock>
</apex:form>
</apex:page>
@isTest
private class AttachClassTest {
//test method
static testMethod void testCase() {
//mock up your test case record here
Case c = New Case (
Subject = 'Test Subject',
Origin = 'PHONE'
);
//reference your extensions
sfdevcaseattachment ext = new sfdevcaseattachment(new Apexpages.Standardcontroller(c));
//now set your test values for your attachments
ext.fileBody = Blob.valueOf( 'this is an attachment test' );
ext.fileName = 'TEST NAME';
//now execute the pagereference by calling the save method
ext.save();
}
}
Hope this make sense. Just remember test methods are almost a mock of what a user would do in the system.
You are the ABSOLUTE best! I woudl have wasted many more days trying to figure that one out, and the way you wrote it to use my examples made much more sense to me and I think I now understand what is needed to create a test class. I have two other controller extentions for this project that I will now go and try to build based off of your great help! Thank you again!
Shawn