You need to sign in to do that
Don't have an account?
Bryan Jimenez 5
Having trouble getting past 46% coverage
Hi Guys,
Attached is the controller and below that is the test class.
Test Class
Any help is greatly appreciated
Attached is the controller and below that is the test class.
global with sharing class ModalFlowAttachment{ global Flow.Interview.attachmentFlow2 myflow { get; set; } global string varAttachmentParentId = 'init'; global ModalFlowAttachment(){ myflow = new Flow.Interview.attachmentFlow2(new map<string, object>{'varAttachmentParentId'=> ''}); } global string getVarAttachmentParentId() { if(varAttachmentParentId == 'init'){ varAttachmentParentId = ''; return ''; } return myflow.varAttachmentParentId; } global Attachment attachment { get { if (attachment == null) attachment = new Attachment(); return attachment; } set; } global PageReference upload() { attachment.OwnerId = UserInfo.getUserId(); attachment.IsPrivate = true; attachment.parentId = getVarAttachmentParentId(); try { insert attachment; } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment')); return null; } finally { attachment = new Attachment(); } ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully')); varAttachmentParentId = null; return null; } }
Test Class
@isTest Private class ModalFlowAttachmentTest { @isTest static void CreateProperty() { McLabs2__Property__c Property = New McLabs2__Property__c(); Property.Name = 'Fakename'; Property.McLabs2__Property_Address__c = 'FakeAddress'; Property.McLabs2__City__c = 'FakeCity'; Property.Block__c = '328'; Property.Lot__c = '23'; Insert Property; } static testMethod void ModalFlowAttachment_1() { system.Test.setCurrentPage(Page.ModalFlowAttachmentVF); ModalFlowAttachment controller = new ModalFlowAttachment(); controller.getVarAttachmentParentId(); Attachment tmpAttach = controller.attachment; Attachment attachment = new Attachment(); attachment.Body = Blob.valueOf('Dreamforce15'); attachment.Name = String.valueOf('Coastal Cloud.txt'); attachment.ParentId = [select id from McLabs2__Property__c limit 1].id; controller.attachment = attachment; controller.upload(); } }
Any help is greatly appreciated
@testSetup static void CreateProperty() {
instead of @isTest static void CreateProperty() {
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm
My test class now looks like this.
It seems what I am having trouble with is testing the try, catch, finally on the controller class.
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&language=en&type=0