You need to sign in to do that
Don't have an account?
Chris987654321
Writing test methods to execute a Catch
If I have a try/catch block which does an update to the Database, how do I write a test method to execute the catch? It seems I would have to force the update to fail?
//Save the information entered in by the student
try {
//save the changes that the student made on the form
update enrollment;
}
catch(System.DMLException e) {
ApexPages.addMessages(e);
System.debug(e);
return null;
}
Hey
There are a few ways to force a DMLException on update. I don't know all of them but some are:
1. Duplicate a unique field in a record eg. insert a record with a certain name, and then try update another with the same name
2. Don't specify a required field.
3. Delete the record before you try the update.
4. Specify the incorrect field type eg. try to put a string in a number field (not 100% sure this is a DML exception. Might be some sort of cast exception).
Ask yourself what conditions did you anticipate in writing this exception, and then try fake them.
Cheers,
Wes