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
Ross Gilbert 8Ross Gilbert 8 

need test class

Hi,

I've got a visualforce page with a custom controller.  This page simply displays the current user's open leads.  It does not do any DML at all, it simply displays existing leads on a visualforce page.

What I need is a test class for this page and its custom controller.

I'm not sure how to write a test class for this kind of thing.

Here's the visualforce page, called "myLeadsView1":
 
<apex:page Controller="myLeadListCtr1">
<apex:form >
    <html>
    &nbsp;&nbsp;&nbsp;<img src="/img/icon/people32.png"/>
    <font size="5">&nbsp;My Open-Uncontacted Leads </font><br></br>
    <h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*Showing my leads, created in the past 6 months, in an 'Open - Not Contacted' status<br></br></h2>
<apex:pageBlock >
    <apex:pageBlockTable value="{!accounts}" var="account">
                                    
                                    <apex:column >
                                    <apex:outputlink value="/{!account.Id}">{!account.LastName}</apex:outputLink>
                                    <apex:facet name="header"> Lead Last Name </apex:facet>     
                                    </apex:column>                                                                        
                                    
                                    <apex:column >
                                    <apex:outputlink value="/{!account.Id}">{!account.FirstName}</apex:outputLink>
                                    <apex:facet name="header"> Lead First Name </apex:facet>     
                                    </apex:column>      
                                    
                                <apex:column > 
                                    <apex:outputField value="{!account.Response_Needed__c}" />
                                    <apex:facet name="header"> Urgency                                      
                                    </apex:facet>                                                                      
                                </apex:column>                                                                   

                                <apex:column >
                                    <apex:outputLink value="/{!account.Id}">{!account.Status}</apex:outputLink>
                                    <apex:facet name="header"> Status</apex:facet>
                                </apex:column>                                 
                                                                         
        <apex:column value="{!account.Email}"/>
        <apex:column value="{!account.Phone}"/>
        <apex:column value="{!account.CreatedDate}"/>

    </apex:pageBlockTable>
                    <br></br>                 
                    <apex:outputLink target="_blank" value="https://cs15.salesforce.com/00Oe0000000Tb5X" id="theLink">View All My Open  Leads</apex:outputLink>
</apex:pageBlock>
</html>
</apex:form>
</apex:page>

Here's the customer controller:
 
public with sharing class myLeadListCtr1 {

    public List<Lead> getAccounts()
    {
        String userId=UserInfo.getUserId();
        UserId=userId.Substring(0,15);
        return [Select Id,Response_Needed__c,FirstName,LastName,Name,Email,Phone,CreatedDate,Status from Lead WHERE (Status = 'Open - Not Contacted' ) AND CreatedDate = LAST_N_DAYS:180 AND OwnerId =: userId ORDER BY CreatedDate DESC LIMIT 1000  ];
    }

}

Does anyone know how I might be able to write a good test class for this code?  Since it does no DML, I don't really know how to test this "code".  I put code in parentheses since it's not really code, but rather just a visualforce page that displays existing data.  

Thanks guys.


 
Cyrus TalladenCyrus Talladen
The test class for this is simple.  You only have to cover the 4 lines inside your controller by calling myLeadListCtrl.getAccounts() . But before that you have to create the test Lead with the appropriate values.  For example: 
 
public static testMethod void tm1(){

   Lead l = new Lead(Status = Open not conatctd, CreatedDate = Date(....) )
   insert l;

   List<Lead> l2 = myLeadListCtrl.getAccounts();
 
 
David ZhuDavid Zhu
Here you go.
@istest

public class myLeadList_Test {
    public static testmethod void MyLeadTest1(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u = new User(Alias = 'qwer', Email='standarduser@testorg.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorgasdf.com');
        
        system.runas(u)
        {
            list<lead> ls = new list<lead>();
            for (integer i=0;i<10;i++)
            {
                lead l = new Lead(status = 'Open - Not Contacted',lastname='tester',company='aaa',phone='1234');
                ls.add(l);
            }
            for (integer i=0;i<10;i++)
            {
                lead l = new Lead(status = 'Open',lastname='tester',company='aaa',phone='1234');
                ls.add(l);
            }
            
            insert ls;
            
            myLeadListCtr1 mlc = new myLeadListCtr1();
            list<lead> mls =mlc.getAccounts();
            system.assertEquals(10, mls.size());
        }
    }
}