function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bryan Jimenez 5Bryan Jimenez 5 

Having trouble getting past 46% coverage

Hi Guys,

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
 
Alain CabonAlain Cabon
Hi,

@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
Bryan Jimenez 5Bryan Jimenez 5
Hi Alain,

My test class now looks like this.
 
@isTest
Private class ModalFlowAttachmentTest {
    
    @testSetup 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();
     
  }
}

It seems what I am having trouble with is testing the try, catch, finally on the controller class.
Alain CabonAlain Cabon
You can view code coverage in several places in the Developer Console. There is a button on the upper left.

https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&language=en&type=0