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
Sana123Sana123 

how can i write test class for dml?


public with sharing class HelloFirst {

  
    public static void  demo1 (Integer intNum) {

        List<Account> listOfAccount = new List<Account>();
        for(integer i=1; i<=intNum; i++){
        Account acct = new account();

        acct.name ='sha'+i;

          listOfAccount.add(acct);
        }
        insert listOfAccount;
        system.debug(listOfAccount);
        }


        public static void demo2 (Integer numAccInsert , Map<Integer,Integer> mapOfContact){
   
            list<Account> listOfAcc = new list<Account>();
            list<Contact> listOfCon = new list<Contact>();
    
            for(Integer i = 1; i<=numAccInsert; i++){
               
                Account objAcc = new Account();
                objAcc.name = 'Anjana' +i;
                listOfAcc.add(objAcc);
            }
            insert listOfAcc;
           
            system.debug(listOfAcc);
      
           
            for(Integer j = 1; j< listOfAcc.size(); j++){

                if(mapOfContact.containsKey(j))
          {
             Id accId = listOfAcc[j].id;
             for(Integer k=1; k<=mapOfContact.get(j); k++)
         {
          Contact objCon = new Contact();
          objCon.AccountId = accId ;
          objCon.lastName = 'Sharma'+k;
          listOfCon.add(objCon);    
            }
        }
    }
            insert listOfCon;
            system.debug(listOfCon);
    
           
        }
     public static void demo3(List<Account>listOfAccount , List<Contact>conc){
         
             List<Contact>listOfContact = new List<Contact>();      
            Database.SaveResult[] srList = Database.insert(listOfAccount, false);
                 for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
               
               for(Contact objconc :conc){                   
                   objconc.AccountId = sr.id;    
               }
              // system.debug('conc---'+conc);
                Database.upsert(conc, false);
            } 
               else 
               {
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Contact fields that affected this error: ' + err.getFields());                    
                    Error_Log__c a1 = new Error_Log__c(Parent_Record_Name__c = '' , Parent_Record_Id__c = '' ,Error_Details__c = err.getStatusCode() + ': ' + err.getMessage());
                    database.upsert(a1 , false);
             }
               }
                 }
                   system.debug(listOfContact);
     }

}
Best Answer chosen by Sana123
CharuDuttCharuDutt
Hii Sana
Try Below Test Class
@isTest
public class campaigntrigger12Test {
@isTest
    public Static void UnitTest(){
        
        Map<Integer,Integer> mapRec = new Map<Integer,Integer>();
        mapRec.put(2,2);
        
       
        list<Account> lstAcc= new list<Account>();
        list<Contact> lstCon = new list<contact>();
        
            Account Acc = new Account();
            Acc.Name = 'Test';
           
       
        Insert lstAcc;
      
            Contact Con = new Contact();
            Con.FirstName = 'Test ';
            Con.LastName = 'Con';
            Con.AccountId = Acc.Id;
                
      
        
        Insert lstCon;
        lstCon.Add(Con);
         lstAcc.Add(Acc);
        HelloFirst.demo1(1);
        HelloFirst.demo3(lstAcc, lstCon);
        HelloFirst.demo2(2 , mapRec);
     }
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

CharuDuttCharuDutt
Hii Sana
Try Below Test Class
@isTest
public class campaigntrigger12Test {
@isTest
    public Static void UnitTest(){
        
        Map<Integer,Integer> mapRec = new Map<Integer,Integer>();
        mapRec.put(2,2);
        
       
        list<Account> lstAcc= new list<Account>();
        list<Contact> lstCon = new list<contact>();
        
            Account Acc = new Account();
            Acc.Name = 'Test';
           
       
        Insert lstAcc;
      
            Contact Con = new Contact();
            Con.FirstName = 'Test ';
            Con.LastName = 'Con';
            Con.AccountId = Acc.Id;
                
      
        
        Insert lstCon;
        lstCon.Add(Con);
         lstAcc.Add(Acc);
        HelloFirst.demo1(1);
        HelloFirst.demo3(lstAcc, lstCon);
        HelloFirst.demo2(2 , mapRec);
     }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer
Sana123Sana123
Actually i want to make test classes by using the assert and the test.startTest() or test.stopTest()
 
Sana123Sana123
Can you please explain that what do you mean by the assert's and teststart or stop ...actualy i a new to it so i cant understand if the test classes are make without it than why we use it
Sana123Sana123
And the code is not running succesfully