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
Harry AustinHarry Austin 

How TestClass executes the logic written in the Class without any conenction made between the "TestClass" and "Class"

Hello,

I have created  a class and a trigger seperately for a  custom object. They are working good.  I wrote a TESTCLASS to check wehether my class is working or not. TestClass also successfully executed my method in the testClass with 100% code coverage.

Then, my question is , how my testClass can recognise the "class" particularly to test. I didnt call any class/Method in my TestClass to refer my actual logoc.    
 
Best Answer chosen by Harry Austin
Agustina GarciaAgustina Garcia
First of all, I would really disadvice to write more than one trigger related to the same object (Standard or Custom) because Salesforce never guarantee the order of execution. Maybe first time Trigger1 is executed before Trigger2 and next time Trigger2 is executed before Trigger1. 

Regarding your question. Using your TestClass and having this code as an example:
 
//Class code
public with sharing classA
{
   public static void method1(myCustomObject__c myObj)
   {
      ...
   }

   public static void method2(myCustomObject__c myObj)
   {
      ...
   }
}
And your trigger
//Trigger code
trigger myTrigger on myCustomObject__c (before insert)
{
   for(myCustomObject__c myObj : Trigger.new) //Remember bulkification
   {
         classA.method1(myObj); 
   }
}

Then only method1 would be covered and method2 will not be covered, so % would not be 100%

If your trigger is:
 
//Trigger code
trigger myTrigger on myCustomObject__c (before insert)
{
   for(myCustomObject__c myObj : Trigger.new) //Remember bulkification
   {
         classA.method1(myObj); 
         classA.method2(myOjb);
   }
}

Then your class would be fully covered by the trigger.

If you also have this class
 
//Class code
public with sharing classB
{
   public static void method3(myCustomObject__c myObj)
   {
      ...
   }
}

But you have above trigger, this second class would not be called there so this class would not be covered at all.

Does it make sense?
 

All Answers

Agustina GarciaAgustina Garcia
Hi,

It would be useful to see a pice of code as an example but I can guess something. 

Does your trigger call your class? If so, if in your test you execute a DML (insert / update / delete) and your trigger checks any of them, then, your class will be also called like a cascade way.

If your trigger doesn't have any reference to the class and the testClass either, then I would need to check it to give you a right answer.

BTW, having a testClass with 100% doesn't mean that your class is also covered. When you run a testClass it is covered succesfully. But did you check in the Developer console that also your class is covered?

Hope this helps
Harry AustinHarry Austin
---Class--
public class discountPrice 
    
    {
        public static void discount(Books__c[] bks)
        {
            for(Books__c  b:bks)
            {
                
                if(b.Author_Name__c=='Amrita Pritam')
                {
                b.price__C*=0.8;
               }
               else
                {
                b.Price__c*=0.5;
                }
            }
        }
    }

---Trigger---- 
trigger discountPriceTrigger on Books__c (before insert) {
    
    books__c[] bks=Trigger.new;
    discountPrice.discount(bks);

}

----Test Class ---

​@Istest
private class TestClassPrice 
{
    static testMethod void validatePriceMethod() 
    {
        Books__c k = new Books__c(Name='mera', Price__c=100);
        insert k;
        
        k = [SELECT name,Price__c FROM Books__c WHERE Id =:k.Id];
        
        System.assertEquals(50, k.Price__c);
     }
 }
Agustina GarciaAgustina Garcia
Exactly,

Your trigger makes a call to your class "discountPrice.discount(bks);"

So by the time that your testClass makes an insert, your trigger is called, and your class is also called and covered.

Agustina
Harry AustinHarry Austin
okay, If I write another Trigger on Insert for the same custom object with another class with different functionality on the price__C. Then which class will be called?
Agustina GarciaAgustina Garcia
First of all, I would really disadvice to write more than one trigger related to the same object (Standard or Custom) because Salesforce never guarantee the order of execution. Maybe first time Trigger1 is executed before Trigger2 and next time Trigger2 is executed before Trigger1. 

Regarding your question. Using your TestClass and having this code as an example:
 
//Class code
public with sharing classA
{
   public static void method1(myCustomObject__c myObj)
   {
      ...
   }

   public static void method2(myCustomObject__c myObj)
   {
      ...
   }
}
And your trigger
//Trigger code
trigger myTrigger on myCustomObject__c (before insert)
{
   for(myCustomObject__c myObj : Trigger.new) //Remember bulkification
   {
         classA.method1(myObj); 
   }
}

Then only method1 would be covered and method2 will not be covered, so % would not be 100%

If your trigger is:
 
//Trigger code
trigger myTrigger on myCustomObject__c (before insert)
{
   for(myCustomObject__c myObj : Trigger.new) //Remember bulkification
   {
         classA.method1(myObj); 
         classA.method2(myOjb);
   }
}

Then your class would be fully covered by the trigger.

If you also have this class
 
//Class code
public with sharing classB
{
   public static void method3(myCustomObject__c myObj)
   {
      ...
   }
}

But you have above trigger, this second class would not be called there so this class would not be covered at all.

Does it make sense?
 
This was selected as the best answer
Harry AustinHarry Austin
Thank you