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
sgsssgss 

soql test class

Can anyone help me in test class of following code.??
To retrieve record of contact object.

public with sharing class ContactRecords {
    /**
    Method : getContactRecord() 
    Description : To fetch Id,Name,Department from Contact Object
    */
    public List<Contact> getContactRecord(){
        List<Contact> contactList = [SELECT Id,Name,Department
                                    FROM Contact];
        System.debug(contactList);
        return contactList;
    }
}

//Cover as many test cases as possible.
Thanks 
Best Answer chosen by sgss
Steven NsubugaSteven Nsubuga
Since the class was declared as with sharing, my test cases include some profile testing, comparing standard user and system administrator.
@isTest
private class ContactRecordsTest {
    
    @isTest static void getContactRecordTest() {
        
        String orgId = UserInfo.getOrganizationId(); 
        // create system admin user
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        User adminUser=new User(firstname = 'admin', 
                         lastName = 'User', 
                         email =  'admin@test'  + orgId + '.org', 
                         Username = 'admin@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1',
                         TimeZoneSidKey = 'America/Los_Angeles',
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         Alias = 'admin', 
                         ProfileId = pf.Id
                        );  
        insert adminUser;
        
        
        // create standard user
        Profile pf2= [Select Id from profile where Name='Standard User']; 
        User standardUser=new User(firstname = 'standard', 
                         lastName = 'User', 
                         email =  'standardUser@test' + orgId  + '.org', 
                         Username = 'standardUser@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1',
                         TimeZoneSidKey = 'America/Los_Angeles',
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         Alias = 'suser', 
                         ProfileId = pf2.Id
                        );  
        insert standardUser;     
        
        
        System.runAs(standardUser) {
            List<Contact> testContacts = new List<Contact>();
            for(Integer i=0;i<2;i++) {
                testContacts.add(new Contact(LastName = 'standardUser'+i, Department  = 'standardUser'));
            }
            insert testContacts;
        }
        System.runAs(adminUser) {
            List<Contact> testContacts = new List<Contact>();
            for(Integer i=0;i<2;i++) {
                testContacts.add(new Contact(LastName = 'adminUser'+i, Department  = 'adminUser'));
            }
            insert testContacts;
        }
        
        // standard user can only access 2 contacts
        System.runAs(standardUser) {
            System.assertEquals(2, new ContactRecords().getContactRecord().size());
        }
        
        // system administratot user can access all 4 contacts
        System.runAs(adminUser) {
            System.assertEquals(4, new ContactRecords().getContactRecord().size());
        }
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
@isTest
public class TestContactRecords 
{
@isTest
public static void testContact ()
{
Account testAccount = new Account();
	testAccount.Name='Test Account' ;
	insert testAccount;
	
	Contact cont = new Contact();
	cont.FirstName='Test';
	cont.LastName='Test';
	cont.Accountid= testAccount.id;
	insert cont;
	
	Test.StartTest(); 
		ContactRecords   obj = new ContactRecords ();
		obj.getContactRecord();
		 
		
	Test.StopTest();
}
}

 
Steven NsubugaSteven Nsubuga
Since the class was declared as with sharing, my test cases include some profile testing, comparing standard user and system administrator.
@isTest
private class ContactRecordsTest {
    
    @isTest static void getContactRecordTest() {
        
        String orgId = UserInfo.getOrganizationId(); 
        // create system admin user
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        User adminUser=new User(firstname = 'admin', 
                         lastName = 'User', 
                         email =  'admin@test'  + orgId + '.org', 
                         Username = 'admin@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1',
                         TimeZoneSidKey = 'America/Los_Angeles',
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         Alias = 'admin', 
                         ProfileId = pf.Id
                        );  
        insert adminUser;
        
        
        // create standard user
        Profile pf2= [Select Id from profile where Name='Standard User']; 
        User standardUser=new User(firstname = 'standard', 
                         lastName = 'User', 
                         email =  'standardUser@test' + orgId  + '.org', 
                         Username = 'standardUser@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1',
                         TimeZoneSidKey = 'America/Los_Angeles',
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         Alias = 'suser', 
                         ProfileId = pf2.Id
                        );  
        insert standardUser;     
        
        
        System.runAs(standardUser) {
            List<Contact> testContacts = new List<Contact>();
            for(Integer i=0;i<2;i++) {
                testContacts.add(new Contact(LastName = 'standardUser'+i, Department  = 'standardUser'));
            }
            insert testContacts;
        }
        System.runAs(adminUser) {
            List<Contact> testContacts = new List<Contact>();
            for(Integer i=0;i<2;i++) {
                testContacts.add(new Contact(LastName = 'adminUser'+i, Department  = 'adminUser'));
            }
            insert testContacts;
        }
        
        // standard user can only access 2 contacts
        System.runAs(standardUser) {
            System.assertEquals(2, new ContactRecords().getContactRecord().size());
        }
        
        // system administratot user can access all 4 contacts
        System.runAs(adminUser) {
            System.assertEquals(4, new ContactRecords().getContactRecord().size());
        }
        
    }
}

 
This was selected as the best answer
sgsssgss
Can anyone give a negativetest code for this case?