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
Solution ArchiveSolution Archive 

Basic test class

Hello,

I have a very basic class and i need to write test class for the same. Can anyone help me understand how to write a test class for this basic getter method in my class.

 

public with sharing class LinksForContacts {
    Public List<Contact> getAllLinks(){
       List<Contact> theLinks = [Select Title,Link_to_this_Idea__c from Contact];
       return theLinks;  
    }
}

 Thanks a lot!!!!!!!!!!!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

It should just be as simple as instantiating the class, executing the getter and asserting you get some response.  Something like:

 

@IsTest
private class TestLinksForContacts
{
   private static testMethod void testGetter()
   {
      LinksForContacts lfc=new LinksForContacts();
      List<Contact> contLinks=lfc.getAllLinks();
      System.assert(contLinks.size()>0);
   }
}

I'm duty bound to point out that the query includes all contacts in the system, so if you have large volumes of data you may hit problems.

All Answers

bob_buzzardbob_buzzard

It should just be as simple as instantiating the class, executing the getter and asserting you get some response.  Something like:

 

@IsTest
private class TestLinksForContacts
{
   private static testMethod void testGetter()
   {
      LinksForContacts lfc=new LinksForContacts();
      List<Contact> contLinks=lfc.getAllLinks();
      System.assert(contLinks.size()>0);
   }
}

I'm duty bound to point out that the query includes all contacts in the system, so if you have large volumes of data you may hit problems.

This was selected as the best answer
Devendra@SFDCDevendra@SFDC

Hi,

 

Try below code.

 

public with sharing class LinksForContacts {
    Public List<Contact> getAllLinks(){
       List<Contact> theLinks = [Select Title,Link_to_this_Idea__c from Contact];
       return theLinks;  
    }

/* Test Method Code*/
 static testMethod void LinksForContactsTest()
         {
           Contact testContact=new Contact(Name='Test Contact 1',--Insert Required Fields--);
		insert testContact;

		LinksForContacts obj=new LinksForContacts();
		obj.getAllLicks();
	}	

/* End Test Method*/
}

 Hope it helps.

 

Regards,

Devendra S

Pat PattersonPat Patterson

I guess Bob, Devendra and I were working in parallel... My solution also includes test.startTest() and test.stopTest() to set up the testing limit context:

 

@isTest
public class TestLinksForContacts {
    static testMethod void testGetAllLinks() {
        // Create some test data
        List<Contact> contacts = new List<Contact>{};
        for (Integer i = 0; i < 10; i++) {
            Contact c = new Contact(LastName = 'Test Contact' + i, Title = 'Test ' + i,
Link_to_this_Idea__c = 'Link ' + i); contacts.add(c); } // Insert the test data - it will not be committed to the database, // since this is a test method insert contacts; // We're testing an instance method, so we need an instance LinksForContacts links = new LinksForContacts(); // This sets up a new governor limits context, so we have accurate // data on the method being tested test.startTest(); List<Contact> linkContacts = links.getAllLinks(); // Switch limit context back to the test method test.stopTest(); // Create a map from the list of contacts, so we can easily check // that our inserted contacts are there Map<ID, Contact> contactMap = new Map<ID, Contact>(linkContacts); // Check that all the contacts we inserted were returned for (Contact c: contacts) { System.assert(contactMap.containsKey(c.id)); } } }

This article is a great resource for writing test methods - http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

Cheers,

 

Pat

Solution ArchiveSolution Archive

Hello All,

 

Thanks you so much to all three of you for clear explanation on testing the getter method.