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
bouscalbouscal 

Not enough coverage testing controller extension

Here's my code, page, extension & test.  No coverage for the entire list of attachments.
Extension:
public class CaseListController {
    
    private final Case cas; 
    private String sortOrder = 'CreatedDate'; 
    public Case cCas;
    public Case pCas; 
    public Id theParent;
    
    public CaseListController(ApexPages.StandardController stdController) {
        this.cas = (Case)stdController.getRecord();
        cCas=[SELECT id, parentid, casenumber FROM case WHERE id=:cas.id];
        pCas=[SELECT id FROM case WHERE id=:cCas.ParentId]; 
        theParent=pCas.id;
        System.debug('The Parent Record is: ' + theParent); 
    }
    
    public List<Attachment> getAttachments() {
        List<Attachment> results; 
        If(theParent != null){
            results = Database.query(
                'SELECT Id, parentid, parent.name, name, CreatedDate, ContentType, ownerid ' + 
                'FROM Attachment ' + 
                ' WHERE parentid =:theParent ' + 
                'ORDER BY ' + sortOrder + ' DESC ' + 
                'LIMIT 10'
            ); 
        } 
        return results;
    }
}
Page: 
<apex:page standardcontroller="Case" extensions="CaseListController" sidebar="false" showHeader="false" >
    <apex:form >
        <apex:pageBlock title="Parent Attachments" id="attachments_list"> 
            <apex:pageBlockTable value="{! attachments }" var="at">
                <apex:column headervalue="File">
                    <apex:outputLink value="/servlet/servlet.FileDownload?file={! at.id}" target="_blank">{! at.Name}
                    </apex:outputLink>
                </apex:column>
                <apex:column value="{! at.contenttype }"/>
                <apex:column value="{! at.createddate }"/>
                <apex:column value="{! at.ownerid }"/>
                <apex:column value="{! at.parent.name }" headerValue="Case"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Test:  
@isTest
public class CaseListController_Test {

    public static testMethod void testMyController() {
        Account testAccount = new Account();
		testAccount.Name='Test Account'; 
        testAccount.Phone='(999) 999-9999';
        
        Case ParentCase = new Case();
        ParentCase.AccountId = testAccount.id; 
        ParentCase.RecordTypeId='012L000000073y2';
        ParentCase.Reason__c='Move User';
        ParentCase.Subject = 'Test Parent Case'; 
        ParentCase.Description = 'Test Parent Case'; 
        insert ParentCase; 
        
        Case ChildCase = new Case();
        ChildCase.AccountId = testAccount.id; 
        ChildCase.ParentId = ParentCase.id;
        ChildCase.RecordTypeId='012L000000073y2';
        ChildCase.Reason__c='Move User';
        ChildCase.Subject = 'Test Parent Case'; 
        ChildCase.Description = 'Test Parent Case'; 
        insert ChildCase; 
        
        Attachment atch = new Attachment();
        Blob bodyBlb=Blob.valueOf('test body');
        atch.Name='test attachment'; 
        atch.parentid=ParentCase.Id; 
        atch.Body=bodyBlb;
        insert atch;
        
        Test.startTest();
        PageReference pageRef = Page.ParentCaseAttachments;
        pageRef.getParameters().put('Id',ChildCase.Id);
        Test.setCurrentPage(pageRef);
        
        ApexPages.StandardController sc = new ApexPages.StandardController(ChildCase);
        CaseListController ext = new CaseListController(sc);
        Test.stopTest();
    }
}

Any direction is appreciated!
Best Answer chosen by bouscal
Amit Chaudhary 8Amit Chaudhary 8
Hi bouscal ,

I found all below issue in your code
1) You need to insert Account record. testAccount
2) You need to call getAttachments(); method in test class.
3) Remove Hard Coded recordTypeId and you can try below code for same
ParentCase.RecordTypeId=Schema.SObjectType.Case.getRecordTypeInfosByName().get('NameOfRecordType').getRecordTypeId();
4) Add Assert in Test class as well

Try below code
@isTest
public class CaseListController_Test 
{
    public static testMethod void testMyController() 
	{
        Account testAccount = new Account();
			testAccount.Name='Test Account'; 
			testAccount.Phone='(999) 999-9999';
		insert testAccount;
        
        Case ParentCase = new Case();
			ParentCase.AccountId = testAccount.id; 
			ParentCase.RecordTypeId='012L000000073y2';
			ParentCase.Reason__c='Move User';
			ParentCase.Subject = 'Test Parent Case'; 
			ParentCase.Description = 'Test Parent Case'; 
        insert ParentCase; 
        
        Case ChildCase = new Case();
			ChildCase.AccountId = testAccount.id; 
			ChildCase.ParentId = ParentCase.id;
			ChildCase.RecordTypeId='012L000000073y2';
			ChildCase.Reason__c='Move User';
			ChildCase.Subject = 'Test Parent Case'; 
			ChildCase.Description = 'Test Parent Case'; 
        insert ChildCase; 
        
        Attachment atch = new Attachment();
			Blob bodyBlb=Blob.valueOf('test body');
			atch.Name='test attachment'; 
			atch.parentid=ParentCase.Id; 
			atch.Body=bodyBlb;
        insert atch;
        
        Test.startTest();
		
			PageReference pageRef = Page.ParentCaseAttachments;
			pageRef.getParameters().put('Id',ChildCase.Id);
			Test.setCurrentPage(pageRef);
			
			ApexPages.StandardController sc = new ApexPages.StandardController(ChildCase);
			CaseListController ext = new CaseListController(sc);
			ext.getAttachments();
		
        Test.stopTest();
    }
}

Let us know if this will help you
 

All Answers

Raj VakatiRaj Vakati
try this code .you need to call  ext.getAttachments() method 
@isTest
public class CaseListController_Test {

    public static testMethod void testMyController() {
        Account testAccount = new Account();
		testAccount.Name='Test Account'; 
        testAccount.Phone='(999) 999-9999';
        
        Case ParentCase = new Case();
        ParentCase.AccountId = testAccount.id; 
        ParentCase.RecordTypeId='012L000000073y2';
        ParentCase.Reason__c='Move User';
        ParentCase.Subject = 'Test Parent Case'; 
        ParentCase.Description = 'Test Parent Case'; 
        insert ParentCase; 
        
        Case ChildCase = new Case();
        ChildCase.AccountId = testAccount.id; 
        ChildCase.ParentId = ParentCase.id;
        ChildCase.RecordTypeId='012L000000073y2';
        ChildCase.Reason__c='Move User';
        ChildCase.Subject = 'Test Parent Case'; 
        ChildCase.Description = 'Test Parent Case'; 
        insert ChildCase; 
        
        Attachment atch = new Attachment();
        Blob bodyBlb=Blob.valueOf('test body');
        atch.Name='test attachment'; 
        atch.parentid=ParentCase.Id; 
        atch.Body=bodyBlb;
        insert atch;
        
        Test.startTest();
        PageReference pageRef = Page.ParentCaseAttachments;
        pageRef.getParameters().put('Id',ChildCase.Id);
        Test.setCurrentPage(pageRef);
        
        ApexPages.StandardController sc = new ApexPages.StandardController(ChildCase);
        CaseListController ext = new CaseListController(sc);
		ext.getAttachments();
        Test.stopTest();
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Hi bouscal ,

I found all below issue in your code
1) You need to insert Account record. testAccount
2) You need to call getAttachments(); method in test class.
3) Remove Hard Coded recordTypeId and you can try below code for same
ParentCase.RecordTypeId=Schema.SObjectType.Case.getRecordTypeInfosByName().get('NameOfRecordType').getRecordTypeId();
4) Add Assert in Test class as well

Try below code
@isTest
public class CaseListController_Test 
{
    public static testMethod void testMyController() 
	{
        Account testAccount = new Account();
			testAccount.Name='Test Account'; 
			testAccount.Phone='(999) 999-9999';
		insert testAccount;
        
        Case ParentCase = new Case();
			ParentCase.AccountId = testAccount.id; 
			ParentCase.RecordTypeId='012L000000073y2';
			ParentCase.Reason__c='Move User';
			ParentCase.Subject = 'Test Parent Case'; 
			ParentCase.Description = 'Test Parent Case'; 
        insert ParentCase; 
        
        Case ChildCase = new Case();
			ChildCase.AccountId = testAccount.id; 
			ChildCase.ParentId = ParentCase.id;
			ChildCase.RecordTypeId='012L000000073y2';
			ChildCase.Reason__c='Move User';
			ChildCase.Subject = 'Test Parent Case'; 
			ChildCase.Description = 'Test Parent Case'; 
        insert ChildCase; 
        
        Attachment atch = new Attachment();
			Blob bodyBlb=Blob.valueOf('test body');
			atch.Name='test attachment'; 
			atch.parentid=ParentCase.Id; 
			atch.Body=bodyBlb;
        insert atch;
        
        Test.startTest();
		
			PageReference pageRef = Page.ParentCaseAttachments;
			pageRef.getParameters().put('Id',ChildCase.Id);
			Test.setCurrentPage(pageRef);
			
			ApexPages.StandardController sc = new ApexPages.StandardController(ChildCase);
			CaseListController ext = new CaseListController(sc);
			ext.getAttachments();
		
        Test.stopTest();
    }
}

Let us know if this will help you
 
This was selected as the best answer
bouscalbouscal
Thank you both!
Amit, inserting the account was certainly the first issue.  I'll chalk that up to lack of experience.
I'm not done with it yet but that's put me on the right path, thank you!  Now to handle an empty list.