0

I am new in the apex and I don't know how to started to create a test class for apex trigger handler. So those anyone here how to create a sample test class of my apex trigger handler small idea may help y understanding. I hope someone will help me. by the way here my code below.

public class TriggerHandler {

public void method(List<Object> usList){

    Set<Id> ids = new Set<Id>();
    for (Object us : usList) {
    ids.add(us.Id);
    }

    List<Object> id = [...some query codes...];
    Object2[] recList = new List<Object2>();
    if(ids.size()>0) {
    RecordType type1 =  [...some query codes...];
        for (Object record : id) {

            Object2 newRec = new Object2(...some query codes...);
            recList.add(newRec);

        insert recList; 
        }
    }
}

}
3

1 Answer 1

-3

You have to insert data as per your need. In your Class pls remove insert statement in for loop it will throw exception.

   @isTest
     private class TestTriggerHandler {
          static testMethod void testInsertAccount(){

         List<Account> accList = new List<Account>();
         for(Integer i = 0 ; i<10 ; i++){
              Account acc = new Account();
              acc.name = 'test'+i;
              accList.add(acc);
         }
       if(accList != null){
          insert accList;
      }

      TriggerHandler.method(accList);
     //use assert to check functionality working fine or not.
     System.assert(***what you want to check***);
 }
}         
5
  • thanks for the idea, it's working now. fixed the problem thanks for help
    – zane
    Jan 30, 2019 at 7:40
  • 1
    Downvoting because this is largely a code dump. Your statement about an insert in a for loop throwing an exception is not true all of the time (DML and queries in for loops are still bad though).
    – Derek F
    Jan 30, 2019 at 14:04
  • 1
    @cloudZigZag Your answer could be improved on several levels. Your statement about exceptions is very general, and while it's true in this case, it would be helpful to say why - since inserts in a loop aren't always exception sources. Your null check has no value as accList cannot be null. The testMethod keyword is deprecated. And this answer doesn't provide much explanation, which is why references to teaching resources like Trailhead are valuable in response to this question.
    – David Reed
    Jan 31, 2019 at 12:12
  • Being more clear about my previous comment: The following statement that you made remove insert statement in for loop it will throw exception is not correct 100% of the time. It will throw an exception if you're dealing with more than 100 records in a transaction, or potentially fewer records if other triggers, workflow field updates, or process builder is involved.
    – Derek F
    Jan 31, 2019 at 20:31
  • My comment on DML/Queries in loops was not a reference to your code (which, as you pointed out, does not contain DML/Queries in a loop). The intent there was to say that even if DML/Queries are in a loop and not causing problems, they are a huge risk factor in any code that contains them (and thus bad practice and should be avoided, as your code does).
    – Derek F
    Jan 31, 2019 at 20:31

Not the answer you're looking for? Browse other questions tagged .