+ Start a Discussion

how to cover catch block code in Test class ?

Try to create an exception(DML or Query or the applicable in your code) such that you can catch and use system.assert(true) in the catch block to ensure the exception is raised and caught.

One example is - when you try to enter a record with a required field : 
In the test class, when you create a record, deliberately miss the required field such that an DML except is raised with error : required value missing 

You can then catch and do the system.assert to prove that exception is raised and caught.
Also remember, that if the invalid record is executing other classes. Like two trigger fires when record is updated then the other classes need to be modified too. All classes need to catch this exception.