You need to sign in to do that
Don't have an account?
David Kassouf
APEX Code Testing - UploadAttachmentController
Hello All,
I am sure you see requests all the time to aid in the creation, modification and testing of new APEX code from greenhorns like myself. The code below is complete and helps with a custom attachment object that replaces the standard Notes & Attachments section on Contracts. The code, object and process works great in the Sandbox environment but I am having difficulties testing the code.
I have browsed through the forums looking and have been researching online for best practices on testing APEX coding in general and have been unable to tackle the project fully. If anyone could assist, I would greatly appreciated it!
I am sure you see requests all the time to aid in the creation, modification and testing of new APEX code from greenhorns like myself. The code below is complete and helps with a custom attachment object that replaces the standard Notes & Attachments section on Contracts. The code, object and process works great in the Sandbox environment but I am having difficulties testing the code.
I have browsed through the forums looking and have been researching online for best practices on testing APEX coding in general and have been unable to tackle the project fully. If anyone could assist, I would greatly appreciated it!
public class UploadContractAttachmentController { public String selectedType {get;set;} public String description {get;set;} private Contract contract {get;set;} public String fileName {get;set;} public Blob fileBody {get;set;} public UploadContractAttachmentController(ApexPages.StandardController controller) { this.contract = (Contract)controller.getRecord(); } // creates a new Contract_Attachment__c record private Database.SaveResult saveCustomAttachment() { Contract_Attachment__c obj = new Contract_Attachment__c(); obj.contract__c = contract.Id; obj.description__c = description; obj.type__c = selectedType; // fill out cust obj fields return Database.insert(obj); } // create an actual Attachment record with the Contract_Attachment__c as parent private Database.SaveResult saveStandardAttachment(Id parentId) { Database.SaveResult result; Attachment attachment = new Attachment(); attachment.body = this.fileBody; attachment.name = this.fileName; attachment.parentId = parentId; // inser the attahcment result = Database.insert(attachment); // reset the file for the view state fileBody = Blob.valueOf(' '); return result; } /** * Upload process is: * 1. Insert new Contract_Attachment__c record * 2. Insert new Attachment with the new Contract_Attachment__c record as parent * 3. Update the Contract_Attachment__c record with the ID of the new Attachment **/ public PageReference processUpload() { try { Database.SaveResult customAttachmentResult = saveCustomAttachment(); if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) { ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Could not save attachment.')); return null; } Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId()); if (attachmentResult == null || !attachmentResult.isSuccess()) { ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Could not save attachment.')); return null; } else { // update the custom attachment record with some attachment info Contract_Attachment__c customAttachment = [select id from Contract_Attachment__c where id = :customAttachmentResult.getId()]; customAttachment.name = this.fileName; customAttachment.Attachment__c = attachmentResult.getId(); update customAttachment; } } catch (Exception e) { ApexPages.AddMessages(e); return null; } return new PageReference('/'+contract.Id); } public PageReference back() { return new PageReference('/'+contract.Id); } }
All Answers
Thanks
Choose it as best answer if it has helped you
Thank you for providing the test code above. The test code works but, is providing about 45% coverage. Which is a great improvement over what I was able to conjure up but it still leaves me a little short. I appreciate your help and am wondering what I am missing.
add ucas.back() before 24th Line,
and let me know which lines are covered in ProcessUpload() method
Chidambar,
Thank you again for your help! I have added the ucac.back() before line 24 and there is now 50% Coverage. From what I can see in the developer’s console, the code below is not covered.
I reread your last post and the lines that are covered in ProcessUpload() method are listed below.
there re is an exception being thrown while inserting contract attachment
you need check what was it, by enabling debug logs to your user , run your test > go to debug log > find the exception being thrown
I added myself to the monitored users under debug logs and ran the UploadContractAttachmentController_Tests. All of the debug logs seemed to be sucessfull and did not throw up any errors.
It is like displaying error messages in the visualforce page, i.e, you are hadling exceptions (instead of coming error mails to you)
You need to open the log, and find the errors
Replace above lines in your class with
and Run the test class again (make sure debug logs are with your user)
open the log, use Ctrl+F and search 'Error is Here' (without single quotes)
Hello Chidambar,
I followed exactly what you stated above, thank you for the step by step instructions, and found the following error.
17:17:40.128 (128797367)|VF_PAGE_MESSAGE|Required fields are missing: [Contract__c]
17:17:40.128 (128906135)|EXCEPTION_THROWN|[20]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Contract__c]: [Contract__c]
17:17:40.130 (130569895)|SYSTEM_METHOD_EXIT|[20]|Database.insert(SObject)
17:17:40.130 (130597730)|METHOD_EXIT|[46]|01pM00000003J71|UploadContractAttachmentController.saveCustomAttachment()
17:17:40.130 (130683864)|SYSTEM_METHOD_ENTRY|[69]|ApexPages.addMessages(APEX_OBJECT)
17:17:40.130 (130868872)|SYSTEM_METHOD_EXIT|[69]|ApexPages.addMessages(APEX_OBJECT)
17:17:40.130 (130909851)|SYSTEM_METHOD_ENTRY|[70]|System.DmlException.getMessage()
17:17:40.130 (130966682)|SYSTEM_METHOD_EXIT|[70]|System.DmlException.getMessage()
17:17:40.131 (131026708)|SYSTEM_METHOD_ENTRY|[70]|System.debug(ANY)
17:17:40.131 (131044793)|USER_DEBUG|[70]|DEBUG|Error is Here:Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Contract__c]: [Contract__c]
Contract__c is the Master-Detail(Contract) field that links the attachment record to the associated contract. In the normal workflow I would create a new contact record, which would create the Contract Number. This contract number is used in the Contract__c field to link the new attachment to the record.
I added the updated test class to the sandbox enviroment and made one more addition when an error flaged. The test works great and provides 78% code coverage. Thank you so much for you help, I really could not have done any of this with out it.