+ Start a Discussion
charlie_vcharlie_v 

Help with Unit Test for simple Custom Controller

I've been trying all morning to get this unit test to work, and now after reading through posts have gotten myself totally confused.

 

Here's the controller:

 

public class HeadlineAndComments { List<Headline__c> headlines; public List<Headline__c> getHeadlines() { if(headlines == null) { headlines = [Select h.Number_of_Comments__c, h.Name, h.Most_Recent_Comment__c, h.Item__c, h.Id, h.Headline_Posted_Date__c, h.CreatedDate, (Select Comment_By__c, Email__c, Comment__c, Comment_Brief__c, Comment_Posted_Date__c, Comment_Date__c From Headline_Comments__r WHERE Post_to_Site__c = TRUE ORDER BY Name) From Headline__c h Where Id = :ApexPages.currentPage().getParameters().get('id')]; } return headlines; } }

 

Here's the unit test I've been working on.

 

public static testMethod void testHeadlineAndCommentsController() { // Create new Headline__c Headline__c h1 = new Headline__c(Name='Test Headline',Item__c='Test Item'); insert h1; // Get a page reference to our newly created headline PageReference pg1 = new PageReference('/apex/PublicHeadlinesWithComments4?id='+h1.Id); System.test.setCurrentpage(pg1); Headline__c h2 = new Headline__c(); ApexPages.standardController sc2 = new ApexPages.standardController(h2); HeadlineAndComments cn2 = new HeadlineAndComments(sc2);

 

I thought what I needed to do was:

  1. Create new Headline__c record
  2. Run the query
  3. Assert and see if the ID of the query match the ID of the record I just created

If the logic is correct, then I'm too ignorant on the syntax to understand how to actually get it to test the controller 'HeadlineAndComments'.

 

Help?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
charlie_vcharlie_v

Bob, thank you very much!  That did the trick.

 

Here is the code for the Controller and Unit Test (provides 100% coverage) for anyone else who stumbles across this thread:

 

 

public class HeadlineAndComments {
List<Headline__c> headlines;

public List<Headline__c> getHeadlines() {
if(headlines == null) {
headlines = [
Select h.Number_of_Comments__c, h.Name, h.Most_Recent_Comment__c, h.Item__c, h.Id, h.Headline_Posted_Date__c, h.CreatedDate,
(Select Comment_By__c, Email__c, Comment__c, Comment_Brief__c, Comment_Posted_Date__c, Comment_Date__c
From Headline_Comments__r WHERE Post_to_Site__c = TRUE ORDER BY Name)
From Headline__c h Where Id = :ApexPages.currentPage().getParameters().get('id')];
}
return headlines;

}
public static testMethod void testHeadlineAndCommentsController() {
// Create new Headline__c
Headline__c h1 = new Headline__c(Name='Test Headline',Item__c='Test Item');
insert h1;

// Get a page reference to our newly created headline
PageReference pg1 = new PageReference('/apex/PublicHeadlinesWithComments4?id='+h1.Id);
System.test.setCurrentpage(pg1);

// Fire up the Controller
HeadlineAndComments cn2 = new HeadlineAndComments();

// Execute the controller method
List<Headline__c> headList = cn2.getHeadlines();

// Check list isn't null
System.assert(headList!=null);

// Check list is expected size
System.assertEquals(1, headList.size());

// Check contents of list are as expected
System.assertEquals(headList[0].id, h1.id);
}
}

 

 

 

Message Edited by charlie_v on 12-30-2009 02:35 PM

All Answers

bob_buzzardbob_buzzard

If that's your full controller, then you won't be able to construct it using a standard controller, as you have no constructor with that signature.

 

One thing to remember when testing VF controllers is that you aren't actually rendering a page, thus even though you have called System.test.setCurrentPage, all you've done is set some information into the current page reference.  No page has been generated and no controller code has been called.

 

Thus once you have set up the data and page reference information for your test, you still need to manually invoke the method you wish to test on the controller, and assert on the result.

 

Something like the following:

 

 

public static testMethod void testHeadlineAndCommentsController() { // Create new Headline__c Headline__c h1 = new Headline__c(Name='Test Headline',Item__c='Test Item'); insert h1; HeadlineAndComments cn2 = new HeadlineAndComments(); // execute the controller method List<Headline__c> headList=cn2.getHeadlines(); // assert information about the list here }

 

 

 

charlie_vcharlie_v

OK, that makes sense. 

 

So if I'm understanding you correctly, asserting information about the list would be something along the lines of comparing h1.Id (the Id of the test record created) to the Id returned in the list?

 

I'm getting an error that says:


Save error: Initial term of field expression must be a concrete SObject: LIST:SOBJECT:Headline__c

 

 

public static testMethod void testHeadlineAndCommentsController() {
// Create new Headline__c
Headline__c h1 = new Headline__c(Name='Test Headline',Item__c='Test Item');
insert h1;

// Get a page reference to our newly created headline
PageReference pg1 = new PageReference('/apex/PublicHeadlinesWithComments4?id='+h1.Id);
System.test.setCurrentpage(pg1);

HeadlineAndComments cn2 = new HeadlineAndComments();

// Execute the controller method
List<Headline__c> headList = cn2.getHeadlines();

// Assert information about the list here
System.assertEquals(headList.Id, h1.Id);
}

 

 

 

 

 

Message Edited by charlie_v on 12-30-2009 07:24 AM
bob_buzzardbob_buzzard

That's because your code is looking for an id on the list object (which doesn't exist) rather than the id of the first element.

 

You'd probably want:

 

 


// check list isn't null
System.assert(headList!=null);

// check list is expected size
System.assertEquals(1, headList.size());

// check contents of list are as expected
System.assertEquals(headList[0].id, h1.id);

 

 

 

Message Edited by bob_buzzard on 12-30-2009 07:17 AM
charlie_vcharlie_v

Bob, thank you very much!  That did the trick.

 

Here is the code for the Controller and Unit Test (provides 100% coverage) for anyone else who stumbles across this thread:

 

 

public class HeadlineAndComments {
List<Headline__c> headlines;

public List<Headline__c> getHeadlines() {
if(headlines == null) {
headlines = [
Select h.Number_of_Comments__c, h.Name, h.Most_Recent_Comment__c, h.Item__c, h.Id, h.Headline_Posted_Date__c, h.CreatedDate,
(Select Comment_By__c, Email__c, Comment__c, Comment_Brief__c, Comment_Posted_Date__c, Comment_Date__c
From Headline_Comments__r WHERE Post_to_Site__c = TRUE ORDER BY Name)
From Headline__c h Where Id = :ApexPages.currentPage().getParameters().get('id')];
}
return headlines;

}
public static testMethod void testHeadlineAndCommentsController() {
// Create new Headline__c
Headline__c h1 = new Headline__c(Name='Test Headline',Item__c='Test Item');
insert h1;

// Get a page reference to our newly created headline
PageReference pg1 = new PageReference('/apex/PublicHeadlinesWithComments4?id='+h1.Id);
System.test.setCurrentpage(pg1);

// Fire up the Controller
HeadlineAndComments cn2 = new HeadlineAndComments();

// Execute the controller method
List<Headline__c> headList = cn2.getHeadlines();

// Check list isn't null
System.assert(headList!=null);

// Check list is expected size
System.assertEquals(1, headList.size());

// Check contents of list are as expected
System.assertEquals(headList[0].id, h1.id);
}
}

 

 

 

Message Edited by charlie_v on 12-30-2009 02:35 PM
This was selected as the best answer