0

I have written plenty of unit tests for Triggers. But I am kind of stuck on how to write one out for a simple view controller.

Here is the custom controller for my VF Controller:

public with sharing class NotificationListController {

    private final Account account;

    public List<Account_Notification__c> notifications {get;set;}

    date d = system.today();

    public NotificationListController(){

        account = [SELECT Id FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];


        notifications = [SELECT Account__c, Related_Product__c, Description__c, Type__c, Expiration_Date__c, Active__c 
                         FROM Account_Notification__c 
                         WHERE Account__c = :account.Id 
                         AND Active__c    = true AND (Expiration_Date__c = null OR Expiration_Date__c > :d)  ];
    }

}

What needs to be asserted? Do I just create account notifications and check for their existence?

Thanks y'all.

1
  • Your first query is using the Account Id to get the Account Id. You could remove it and simply use the Account Id directly in the query for your Notifications.
    – dBeltowski
    Sep 16, 2016 at 20:54

1 Answer 1

2

In your case you could write methods for

  1. Instantiate with the ID parameter of Existing ID and notification within expiration dates - Assert the value of Account and notifications are correct
  2. Instantiate without the ID parameter - Assert the value of Account and notifications are correct and that no errors are present
  3. Instantiate with the ID parameter of Existing ID and notification not within expiration dates - Assert the value of Account and notifications are correct
  4. Instantiate with the ID parameter of a NON-existing Existing ID - Assert the value of Account and notifications are correct and no errors
  5. Instantiate with the ID parameter of Existing ID and no notification records existing - Assert the value of Account and notifications are correct

This may seem a bit overboard but if you want to do it right.....(And I could be missing some variations).

I can immediately see that with your current code:

2 and 4 will fail and throw an error

3
  • Overkill is good. Overboard is bad.
    – sfdcfox
    Sep 16, 2016 at 20:36
  • I try to instantiate the controller and pass an account created in the Unit Test but it tells me "Method does not exist or incorrect signature: [NotificationListController].account(Account)". Do you know the issue here?
    – Allen Mann
    Sep 19, 2016 at 19:13
  • @AllenMann ask a new question with your current code
    – Eric
    Sep 19, 2016 at 19:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .