+ Start a Discussion
syricsyric 

Help increasing code coverage for controller

Hello- I'm new to visualforce and this is my first project.  Through the online guides and examples I have been able to accomplish almost everything.  I'm trying to increase my code coverage.  It is currently at 66%.

This is my controller:
public with sharing class CommentsController {

	private final Case cas;
		   
    //
    public String MasterRecordId        {get; set;}
    public List<Case_Comments__c> ChildRecords  {get; set;}
    public Case MasterRecord                 {get; set;}

    //SET UP PAGE
    public CommentsController(ApexPages.StandardController stdController) {
    		this.cas = (Case)stdController.getRecord();
		MasterRecordId = ApexPages.currentPage().getParameters().get('id');

		 //CHECK FOR ID
        if(!String.isBlank(MasterRecordId)){

            //GET PARENT
            MasterRecord =
                [
                    SELECT 
                        Id,
                        CaseNumber
                    FROM
                        Case
                    WHERE
                        Id = :MasterRecordId
                ];

            //GET CHILD RECORDS
            ChildRecords =
                [
                    SELECT 
                        Name,
						Id,
                        CreatedDate,
                        CreatedBy.Id,
                        CreatedBy.Name,
                        Time_Spent__c,
                        Comment__c,
                        ParentId__c
                    FROM
                        Case_Comments__c
                    WHERE 
                        ParentId__c = :MasterRecordId order by CreatedDate desc
                ];
        }
    }

}

This is my test class:

@isTest
	public class testCommentsController {
    	static testMethod void testFirstTest() {

        // CREATE ACCOUNT
		Account newAccount = new Account(Name = 'Test Account', Phone='817-999-9999');
		insert newAccount;
      
        // CREATE CONTACT
		Contact newContact = new Contact(LastName = 'Test Contact', AccountId = newAccount.Id);
		insert newContact;  
		       		        
        // CREATE CASE
        Case newCase = new Case(Subject = 'Case Subject', Description = 'Case Description', ContactId = newContact.Id, AccountId = newAccount.Id);
        insert newCase;  
        
        // CREATE CASE COMMENT
        Case_Comments__c newComment = new Case_Comments__c(ParentId__c = newCase.Id, Comment__c = 'Test Comment Body', Time_Spent__c = .5);
        insert newComment;

        ApexPages.StandardController sc = new ApexPages.StandardController(newCase);
        CommentsController cc = new CommentsController (sc);

        PageReference pageRef = Page.Comments;
        pageRef.getParameters().put('id', String.valueOf(newCase.Id));
        Test.setCurrentPage(pageRef);
			
        }
}

I can tell from the test coverage highlights that the case "MasterRecord" and the list "ChildRecords" from the controller are not covered.  I assume that I would need to assert that MasterRecord = the case, and that ChildRecords ParentId__c = MasterRecord Id.  I do not know how to do that.  I do not know how to reference them in my test class or if that is right.  Any help would be appreciated.

Best Answer chosen by syric
Shyam BhundiaShyam Bhundia
Try moving lines 24 to 26 above line 21.  The reason for this is that you are calling the code which uses the ID in the parameter before its even set in the page parameters, therefore it will never test lines 16 to 47 in your controller.

All Answers

Shyam BhundiaShyam Bhundia
Try moving lines 24 to 26 above line 21.  The reason for this is that you are calling the code which uses the ID in the parameter before its even set in the page parameters, therefore it will never test lines 16 to 47 in your controller.
This was selected as the best answer
syricsyric
Oh.. So just setting the Id for page so everything can run will test it.  I thought for sure I was going to prove it with assert statements.  Thank you for the help.  I appreciate it.
Shyam BhundiaShyam Bhundia
You should still use assert statements to check if the correct data is found.  e.g. check if there's content in ChildRecords.
syricsyric
Could you tell me or point to something that would explain how do I refernce those in the test class?  That is what I didn't know how to do.  Thanks
Shyam BhundiaShyam Bhundia
you access the variables using the instance of the class you created.  

Something like:
//checks if the child records are not null
system.assert(cc.ChildRecords.size  != null);

//checks if the child records list has elements if its not null
if(cc.ChildRecords.size  != null){
    system.assert(!cc.ChildRecords.isEmpty());
}

syricsyric
Last night I was trying:
List<Case_Comments__c> comments = cc.ChildRecords();
		System.assertEquals(1,comments.size());
and I was getting a "Method does not exist or incorrect signature" error, but I understand now.  Thanks again for the help.