Wednesday, May 20, 2015

Sneak into Writing Test Classes

On a sunny day , working from my apartment Balcony :
working on test classes project i have to struggle for code coverage,Though it is said to be simple task but some times found people juggling with obtaining coverage .i would like to introduce some challenges that every developer faces:

a) if a object have many lookup and some complex trigger/classes structure may hit too many soql
b)After trying so many combination of conditional block  can lead  no +ve in code coverage.
c)May hit developer time span to write test class leading migration activity delay
d)In efficient code coverage without writing unit  test for  +ve and -ve scenario.
e) Improper Exceptions Handling


Here are ways to handle:

a/b) If object may have multiple lookups and complex structure with triggers/classe :
-do not provide all lookup at a time since lookup population may be distributed as business

for example : If custom object "aa__c" have lookup let suppose account/contact/case/....it is possible aa__c record may have account/contact for some business and for other business account with other lookup.

So Break single method in multiple as in 1 : populate two lookup or as per your convience in 2: other two....this should be staritgey .

Cover method code in start test/stop test

b) if calling Class method from trigger: in a complex situation while writing test if method not calling -->call that method within testclass while passing argument.

trigger
{
class_abc.pqr(trigger.new);
}

public class class_abc
{
pqr(list<sobject> lst)
}

@test
{
method1()
{
insert record;
list.add(record); // add to list
classinstance.pqr(list);
}
}

c/d)Include if /else condition : set up data for + and - coverage
e)There may be scenarion you are getting last line exception when executing unit test  and after juggling many hours not fixing it up and got no solution in that case use try-catch block.
f) You can call trigger-class methods in test class by passing parameters/argument if any.all method in class will be cover.

Thanks





No comments:

Post a Comment