You need to sign in to do that
Don't have an account?
Harry 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.
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.
Regarding your question. Using your TestClass and having this code as an example:
And your trigger
Then only method1 would be covered and method2 will not be covered, so % would not be 100%
If your trigger is:
Then your class would be fully covered by the trigger.
If you also have this class
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
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
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);
}
}
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
Regarding your question. Using your TestClass and having this code as an example:
And your trigger
Then only method1 would be covered and method2 will not be covered, so % would not be 100%
If your trigger is:
Then your class would be fully covered by the trigger.
If you also have this class
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?