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
Andrew Hoban 6Andrew Hoban 6 

Test Class for VF Page

Hi all,

I have created a test class for a VF page but not sure if it is working. The Apex class we are trying to test is shown below: Thanks

Class
public class EvertonUsersLeavers {

  public List<Everton_Users__c> getEvertonUsersLeavers() {
        return [SELECT Name, Leaver_Date__c, Email_Address__c FROM Everton_Users__c
                WHERE Leaver_Date__c >=  TODAY];
    }
}

Test Class
@isTest
public class EvertonUsersLeaversTest{
    public static testMethod void EvertonUsersLeavers(){
         

        Everton_Users__c USER = new Everton_Users__c(
            Name = 'Test Name',
            Leaver_Date__c = Date.newInstance(2015 , 07 ,15),
            Email_Address__c = 'test@evertonfc.com'
        
        );
        insert USER;

      
    }
}



 
Best Answer chosen by Andrew Hoban 6
Vivek DeshmaneVivek Deshmane
Please try this and let me know.
@isTest
public class EvertonUsersLeaversTest{
    public static testMethod void EvertonUsersLeavers(){
        Test.startTest()    
    
        Everton_Users__c USER = new Everton_Users__c(
            Name = 'Test Name',
            Leaver_Date__c = Date.newInstance(2015 , 07 ,15),
            Email_Address__c = 'test@evertonfc.com'
        
        );
        insert USER;
      EvertonUsersLeavers evntuserObj= new EvertonUsersLeavers()
       System.assertNotEquals(evntuserObj.getEvertonUsersLeavers(),null);
       System.assert(evntuserObj.getEvertonUsersLeavers().size()>0)
      Test.stopTest();    
    }
}
Best Regards,
-Vivek

All Answers

ShaTShaT
Hi Andew,

You havent created class intance in test class.

Try this.

@isTest public class EvertonUsersLeaversTest
{ public static testMethod void EvertonUsersLeavers()
{ Everton_Users__c USER = new Everton_Users__c( Name = 'Test Name', Leaver_Date__c = Date.newInstance(2015 , 07 ,15), Email_Address__c = 'test@evertonfc.com' );
insert USER; } 

EvertonUsersLeavers  EvertonUsersLeaversObj= new EvertonUsersLeavers();
EvertonUsersLeaversObj.getEvertonUsersLeavers();

}

Thanks
ShaT
Vivek DeshmaneVivek Deshmane
Hi Andrew,
Please try below code and let know if it helps.
@isTest
public class EvertonUsersLeaversTest{
    public static testMethod void EvertonUsersLeavers(){
        Test.startTest()    
    
        Everton_Users__c USER = new Everton_Users__c(
            Name = 'Test Name',
            Leaver_Date__c = Date.newInstance(2015 , 07 ,15),
            Email_Address__c = 'test@evertonfc.com'
        
        );
        insert USER;
      EvertonUsersLeavers evntuserObj= new EvertonUsersLeavers()
       System.assertNotEquals(EvertonUsersLeavers.getEvertonUsersLeavers(),null);
       System.assert(EvertonUsersLeavers.getEvertonUsersLeavers().size()>0)
      Test.stopTest();    
    }
}
Please mark the best answer.
Best Regards,
-Vivek
Andrew Hoban 6Andrew Hoban 6
Thanks for the reply. I am now getting the message "Medod must define a body Line 1 Column 1" It is refering to the line:


EvertonUsersLeaversObj.getEvertonUsersLeavers();

Thank you 
Vivek DeshmaneVivek Deshmane
Please try this and let me know.
@isTest
public class EvertonUsersLeaversTest{
    public static testMethod void EvertonUsersLeavers(){
        Test.startTest()    
    
        Everton_Users__c USER = new Everton_Users__c(
            Name = 'Test Name',
            Leaver_Date__c = Date.newInstance(2015 , 07 ,15),
            Email_Address__c = 'test@evertonfc.com'
        
        );
        insert USER;
      EvertonUsersLeavers evntuserObj= new EvertonUsersLeavers()
       System.assertNotEquals(evntuserObj.getEvertonUsersLeavers(),null);
       System.assert(evntuserObj.getEvertonUsersLeavers().size()>0)
      Test.stopTest();    
    }
}
Best Regards,
-Vivek
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Thank you to both, I now have 100% Code Coverage