+ Start a Discussion
Rishabh Patel 1Rishabh Patel 1 

Test class for an Apex class that does basic lead and Event Query( I am bad at test classes)

So I have an apex class that does 6 queries , one on event and rest on leads. 

I call the results of the query on a visual force page. 

Everything is working perfect. The only problem is, I do not know how to create a test class for this. 

Here is my apex Class that does the query  

public with sharing class TestDisplayQueryList{
public List<Event> getEvents {get; set;}
public List<Lead> getLeads {get; set;}
public List<Lead> getlast7Leads{get; set;}
public List<Lead> getlast15Leads{get; set;}
public List<Lead> getlast30Leads{get; set;}
public TestDisplayQueryList(){

//get all future  Events

getEvents = [SELECT StartDateTime,Subject,WhoId FROM Event WHERE OwnerId = :UserInfo.getUserId() AND StartDateTime = TODAY ];

//Get leads Created withing one day

getLeads = [SELECT Id,Name,Status,Phone,Email FROM Lead WHERE CreatedDate = LAST_N_DAYS:1  AND (Status ='New' or Status = 'Assigned' or Status = 'Working') AND OwnerId = :UserInfo.getUserId() LIMIT 5];

//Get leads created within 7 days

getlast7Leads = [SELECT Id,Name,Status,Phone,Email FROM Lead WHERE CreatedDate = LAST_N_DAYS:7  AND (Status ='New' or Status = 'Assigned' or Status = 'Working') AND OwnerId = :UserInfo.getUserId() LIMIT 5];

//Get leads created witing 15 days

getlast15leads= [SELECT Id,Name,Status,Phone,Email FROM Lead WHERE CreatedDate = LAST_N_DAYS:15  AND (Status ='New' or Status = 'Assigned' or Status = 'Working') AND OwnerId = :UserInfo.getUserId() LIMIT 5];

//Get leads Created within 30 days

getlast30leads=[SELECT Id,Name,Status,Phone,Email FROM Lead WHERE CreatedDate = LAST_N_DAYS:30  AND (Status ='New' or Status = 'Assigned' or Status = 'Working') AND OwnerId = :UserInfo.getUserId() LIMIT 5];

}
}


I made few test classes that insert some leads and events , But was not able to go further than that. 

What could be the better approach for the test class for this. 

Best Answer chosen by Rishabh Patel 1
Raj VakatiRaj Vakati
@isTest
private class TestDisplayQueryList_Test  {

    static TestMethod void testEx1()
	{
		Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       date tDate = date.today();
       date uDate = Date.today().addDays(30);
        
         User u = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.Id,
            Username = 'testtermsconditions1234423@demooo.com',
            Alias = 'batman',
            Email='testtermsconditions1234423@demooo.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
          
        
       
        
        System.runas(u) {
			
			
			
		
		
		System.Test.startTest();
		
		Account testAccount1 = new Account(Name = 'TaskMeetingCompleteUnitTest');
insert testAccount1;

Event evet12 = new Event(ActivityDateTime=System.today(),DurationInMinutes=50, WhatId = testAccount1.id);

insert evet12;


			List<Lead> lArr = new List<Lead>(0 ;
			for(Integer i = 0 ; i<45 ;i++){
            	Lead lstLead =   new Lead();
        lstLead.Company = 'JohnMiller';
        lstLead.LastName = 'Mike';
        lstLead.Status = 'Assigned';               
        lArr.add(lstLead);  
			}
			
			insert lArr ;
																	   
			TestDisplayQueryList l = new TestDisplayQueryList();
			
		System.Test.stopTest();
		}
    }
}

 

All Answers

Raj VakatiRaj Vakati
@isTest
private class TestDisplayQueryList_Test  {

    static TestMethod void testEx1()
	{
		Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       date tDate = date.today();
       date uDate = Date.today().addDays(30);
        
         User u = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.Id,
            Username = 'testtermsconditions1234423@demooo.com',
            Alias = 'batman',
            Email='testtermsconditions1234423@demooo.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
          
        
       
        
        System.runas(u) {
			
			
			
		
		
		System.Test.startTest();
		
		Account testAccount1 = new Account(Name = 'TaskMeetingCompleteUnitTest');
insert testAccount1;

Event evet12 = new Event(ActivityDateTime=System.today(),DurationInMinutes=50, WhatId = testAccount1.id);

insert evet12;


			List<Lead> lArr = new List<Lead>(0 ;
			for(Integer i = 0 ; i<45 ;i++){
            	Lead lstLead =   new Lead();
        lstLead.Company = 'JohnMiller';
        lstLead.LastName = 'Mike';
        lstLead.Status = 'Assigned';               
        lArr.add(lstLead);  
			}
			
			insert lArr ;
																	   
			TestDisplayQueryList l = new TestDisplayQueryList();
			
		System.Test.stopTest();
		}
    }
}

 
This was selected as the best answer
Rishabh Patel 1Rishabh Patel 1

Code coverage 100% ! Thanks a lot. Raj, you are a genius! 

Thanks a lot again