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
sfdc18sfdc18 

Test class covering only 70% code

Hi,
I am getting 70% code coverage. How can I improve my code coverage above 75%.
 
Apex Class:

public with sharing class CopyAttachmentsController {

    public Set<string> supportCaseIds = new Set<string>();
    ID l2CaseId; 
    public List<AttachmentWrapper> attList {get; set;}
    
    public CopyAttachmentsController(ApexPages.StandardController controller)
    {
        supportCaseIds.add(ApexPages.CurrentPage().getParameters().get('parentId'));
        system.debug('-----------supportCaseIds------------'+supportCaseIds);

        l2CaseId = ApexPages.CurrentPage().getParameters().get('Id');
        system.debug('-----------l2CaseId------------'+l2CaseId);
    }

    public List<AttachmentWrapper> getAttachments() {
        if(attList == null) {
            attList = new List<AttachmentWrapper>();
            for(Attachment a: [Select Id, Name, Body, ParentId from Attachment Where ParentId in : supportCaseIds]) {
                attList.add(new AttachmentWrapper(a));
            }
        }
        return attList;
    }
 
    public PageReference processSelected() {

        List<Attachment> selectedAttachments = new List<Attachment>();

        for(AttachmentWrapper attach : getAttachments()) {
            if(attach.selected == true) {
                selectedAttachments.add(attach.att);
            }
        }
        List<Attachment> l2AttList = new List<Attachment>();
        for(Attachment attForL2 : selectedAttachments)
        {
            Attachment attForL2Case = New Attachment(Name = attForL2.Name, Body = attForL2.Body);
            attForL2Case.ParentId = ApexPages.CurrentPage().getParameters().get('Id');
            l2AttList.add(attForL2Case);
        }
        
        if (Limits.getHeapSize() > Limits.getLimitHeapSize()) { 
            ApexPages.Message errMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Attachments limit exceeded : Please deselect few attachments and try again');
            ApexPages.addMessage(errMsg); 
            return null;
        }
        
        If(l2AttList.size() > 0)
        {
            insert l2AttList;
        }

        attList=null;

        PageReference pageReference = new PageReference('/'+l2CaseId);
        pageReference.setRedirect(true);
        return pageReference;
    }

    public class AttachmentWrapper {
        public Attachment att {get; set;}
        public Boolean selected {get; set;}

        public AttachmentWrapper(Attachment a) {
            att = a;
            selected = false;
        }
    }
}

Test Class :

@isTest
public class CopyAttachmentsControllerTest {

private  static  RecordType supportCaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                        WHERE Name ='Support Case' 
                                                        AND SObjectType = 'Case'];
                                                        
private  static  RecordType L2CaseRecordType  = [SELECT Name, Id FROM RecordType 
                                                        WHERE Name ='L2 Case' 
                                                        AND SObjectType = 'Case'];

    static testmethod void CopyAttachmentTestCase(){
        Account   testAccount = new Account();
        testAccount.Name  = 'Test Acc';
    
        insert testAccount;
    
        Contact   testContact = new Contact();
        testContact.LastName  = 'Test Name';
        testContact.AccountId = testAccount.Id;
    
        insert testContact;

        Product2  testProduct = new Product2();
        testProduct.Id                    = null;
        testProduct.Name                  = 'Test Product Name';
        testProduct.Product_Category__c   = 'Category';
        testProduct.Product_Family__c     = 'Family';
        testProduct.Product_Sub_family__c = 'Sub-Family';    

        insert testProduct;
    
        Case  testCase  = new Case();
        testCase.RecordTypeId = supportCaseRecordType.Id;
        testCase.Summary__c   = 'Summary';
        testCase.Description  = 'Description';
        testCase.Origin       = 'Email';
        testCase.Status       = 'New';
        testCase.I_Agree__c   = true;
        testCase.ContactId    = testContact.Id;
        testCase.ProductId    = testProduct.Id;

        insert testCase;
        
        Case  testCase2  = new Case();
        testCase2.RecordTypeId = L2CaseRecordType.Id;
        testCase2.Summary__c   = 'Summary2';
        testCase2.Description  = 'Description2';
        testCase2.Origin       = 'Email2';
        testCase2.Status       = 'New2';
        testCase2.I_Agree__c   = true;
        testCase2.ContactId    = testContact.Id;
        testCase2.ProductId    = testProduct.Id;

        insert testCase2;


        Attachment att = new Attachment();
        att.ParentId = testCase.Id;
        att.name = 'test'; 
        att.body = blob.valueof('test');
        
        insert att;

        Attachment attDup = new Attachment();
        attDup.ParentId = testCase2.Id;
        attDup.name = att.name; 
        attDup.body = att.body;
        
        List<Attachment> listatt = new List<Attachment>();
        listatt.add(attDup);
        insert listatt;
        System.assertEquals(1, listatt.size());
        
        Id idRadio;
        ApexPages.StandardController sc = new ApexPages.standardController(testCase);
        CopyAttachmentsController cls = new CopyAttachmentsController(sc);
        cls.processSelected();
        CopyAttachmentsController.AttachmentWrapper innerClass = new CopyAttachmentsController.AttachmentWrapper(att);
        innerClass.selected = true;
        innerClass.att = att;
        system.currentPageReference().getParameters().put('idRadio', testCase.Id);
        System.assert( testCase.Id != null );
        
        List<CopyAttachmentsController.AttachmentWrapper> listInnerClass = new List<CopyAttachmentsController.AttachmentWrapper>();
        listInnerClass.add(new CopyAttachmentsController.AttachmentWrapper(attDup));
        
        ApexPages.Message[] pageMessages = ApexPages.getMessages();
        //System.assertNotEquals(0, pageMessages.size());
        
        Boolean messageFound = false;
        
        for(ApexPages.Message message : pageMessages) {
        if(message.getSummary() == 'Attachment size limit exceeded - please select less no of attachments'
        && message.getDetail() == 'Attachment size limit exceeded - please select less no of attachments'
        && message.getSeverity() == ApexPages.Severity.ERROR) {
        messageFound = true;        
        }
        }
        
        //System.assert(messageFound);
    }
}

 
Alexander TsitsuraAlexander Tsitsura
Hi sfdc,

You can view uncoverage lines, for it use developer console or ide. 
For developer console read documantation https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en (https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en)

User-added image

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex