You need to sign in to do that
Don't have an account?
SFDC-PDX
Help with test class for simple ApexClass + VF Page
I am an experienced Administrator but not a developer. I tried my hand at creating a very simple ApexClass with a Visual Force Page. The page is meant to be embeded on an Asset Page Layout, and return Custom Object Records where the Name matches the Asset's Serial Number. This part works fine, the problem is the test class. I have spent more time than I care to admit (days) trying to figure this out, looking at documentation and code examples. I am at a loss and hoping someone can lend a hand.
Here is my ApexClass:
Attempting to Create an Account, an Asset, and a Dongle_Record__c, and then somehow test the Calss and VF Page.
Here is my ApexClass:
public class DongleRecords { public List<Dongle_Record__c> DongleRecords { get; set; } Public DongleRecords ( ApexPages.StandardController std ) { if( std.getRecord().Id != null ) { Asset con = [ Select Id, SerialNumber from Asset where Id =: std.getRecord().Id ]; DongleRecords = [ Select Name, Application_Name__c, Number_of_Licenses__c, Release_Date__c, Expiry_Date__c, Last_Upgrade_Date__c from Dongle_Record__c where Name =: con.SerialNumber ]; } else { DongleRecords = new List<Dongle_Record__c>(); } } }Here is my VF Page
<apex:page StandardController="Asset" extensions="DongleRecords"> <apex:form > <apex:pageBlock title="Dongle Licenses"> <apex:pageBlockTable value="{!DongleRecords}" var="dg"> <apex:column value="{!dg.Name}"/> <apex:column value="{!dg.Application_Name__c}"/> <apex:column value="{!dg.Number_of_Licenses__c}"/> <apex:column value="{!dg.Release_Date__c}"/> <apex:column value="{!dg.Expiry_Date__c}"/> <apex:column value="{!dg.Last_Upgrade_Date__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>Here is my Test Class so far:
Attempting to Create an Account, an Asset, and a Dongle_Record__c, and then somehow test the Calss and VF Page.
@isTest public class DongleRecordsTest { static testMethod void testMethod1() { Account testAccount = new Account(); testAccount.Name='Test Account' ; insert testAccount; Asset ast = new Asset(); ast.Name ='Test Asset'; ast.SerialNumber ='TEST0000'; ast.accountid = testAccount.id; insert ast; Dongle_Record__c dng = new Dongle_Record__c (); dng.Name = 'TEST0000'; insert dng; Test.StartTest(); ApexPages.currentPage().getParameters().put('id', String.valueOf(ast.Id)); Test.StopTest(); } }Any help would be tremendously appreciated...
Try this class:
Let me know if it helps!
All Answers
In your test class you have to first instantiate the Apexpage.standardcontroller, then pass it as a parameter to the constructor of extension controller and then create a pagereference object(page). And then set the created as current page for test.
Refer this link, it will give you exactly what you need: https://salesforce.stackexchange.com/questions/102740/how-to-write-test-class-for-standard-controller-along-with-extensions
Let me know if you face any problem.
Thanks
What I really need is the test class, I would be soooo happy to buy someone lunch and send them $20 via PayPal for a test class that works.
Try this class:
Let me know if it helps!
I really insist on buying you lunch please :)
I'm not sure how to contact you privately.