+ Start a Discussion
SFDC-PDXSFDC-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:
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...


 
Best Answer chosen by SFDC-PDX
Narender Singh(Nads)Narender Singh(Nads)
Ohh, my apologies. I forgot the part where you mentioned you are not a developer.

Try this class:
 
@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;
       
        ApexPages.StandardController sc = new ApexPages.StandardController(ast); 
        DongleRecords obj= new DongleRecords(sc);

    }
    
   static testMethod void testMethod2(){
        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
              
 
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        DongleRecords obj= new DongleRecords(sc);
    }

}

Let me know if it helps!

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,
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
SFDC-PDXSFDC-PDX
Thank you for the tip and the link. I reviewed the linked post and still can’t make sense of it. Unfortunately this is way over my head, as I mentioned I’m not a developer, and honestly after this experience I don’t think I’ll be tinkering with this again. 

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. 
Narender Singh(Nads)Narender Singh(Nads)
Ohh, my apologies. I forgot the part where you mentioned you are not a developer.

Try this class:
 
@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;
       
        ApexPages.StandardController sc = new ApexPages.StandardController(ast); 
        DongleRecords obj= new DongleRecords(sc);

    }
    
   static testMethod void testMethod2(){
        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
              
 
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        DongleRecords obj= new DongleRecords(sc);
    }

}

Let me know if it helps!
This was selected as the best answer
SFDC-PDXSFDC-PDX
You are a life saver! Now about that lunch, do you have a PayPal address I can have?
I really insist on buying you lunch please :)
I'm not sure how to contact you privately.