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
Rahul Chauhan 33Rahul Chauhan 33 

my requirement is write a test class

My class is:-
public class Contactvfpage
{
    public Map <string,List<Contact>> contactMap {get;set;}
    public Contactvfpage() {
        
      
        contactMap = new Map < string, List < Contact >> ();
        List<Contact> conlist = [Select name,Email,Account.Name from Contact Where Account.Name != null Limit 100];
        list<Contact> tempConList;
        for (Contact con : conlist) {
            if(contactMap.containskey(con.account.Name)) {
                tempConList = contactMap.get(con.account.Name);     
                tempConList.add(con);
            } else {
                tempConList = new list<contact>();
                tempConList.add(con);
            }
            contactMap.put(con.account.Name,tempConList);
        }
    } }

i am trying this code to write its test class :--
@isTest
public class Contactvfpage_Test {
        public static testmethod void testInsertAccount(){
          List<Contact> conlist = new List<Contact>([Select name,Email,Account.Name from Contact Where Account.Name != null Limit 100]);
        
          }
 }

Please help me how to write test class
thanks in advance
Best Answer chosen by Rahul Chauhan 33
Raj VakatiRaj Vakati
Use this code ..100%
@isTest
public class Contactvfpage_Test {
    public static testmethod void testInsertAccount(){
          Account acc=new Account();
       acc.Name='TestAccount';
       Insert acc;
        
        Contact c=new Contact(
            FirstName='fname',accountId = acc.id ,
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
        
        Contact c1=new Contact(
            FirstName='fname',accountId = acc.id ,
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c1; 
        
        Test.StartTest(); 
        
        Contactvfpage refClass = new Contactvfpage();
        
        Test.stopTest(); 
        
        
    }
}