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

Test method on a visualforce extension

I have the following simple visualforce extension, but I'm having a hard time getting the test method to asert properly. Here's the class.

public class extUserProduct{

    private final User_Product__c UserProduct;
    List<User_Product__c> lstUserProduct;
    Contact UserProductNew;
    public extUserProduct(ApexPages.StandardSetController controller) {
        lstUserProduct = (List<User_Product__c>)[Select Name, Id, Custom_User_Fee__c, Custom_Currency_Code__c,
                                Contact_Full_Name__c, Billing_Start_Date__c, Active_Start_Date__c, Contact__c,Product__c, Product_Provisioning_Date__c
                                From User_Product__c
                                where Product_Setup_Complete__c=FALSE AND Contact__c=:ApexPages.currentPage().getParameters().get('id')];  
        UserProductNew = [Select Id, Name From Contact Where Id =: ApexPages.currentPage().getParameters().get('id')];


    public List<User_Product__c> getlstUserProduct(){
        return lstUserProduct;
    public Contact getUserProductNew(){
        return UserProductNew;
    public PageReference save() {
            update lstUserProduct;
        catch(DmlException ex){
            return Null;
            return (new ApexPages.StandardController(UserProductNew)).view();

I've started my test, but can't figure out how to assert the class to get any test coverage. Here's the test so far.
@isTest private class TestUserProd {

    static testMethod void TestUserProd() {
        City__c ct = new City__c(name='Toronto');
        insert ct;
        Account a = new Account(name='Test', Type='Customer');
        insert a;

        Contact c = new Contact(, FirstName='Test', LastName='Test', phone='555-555-5555');
        insert c;
        User_Product__c up = new User_Product__c(,, Product__c='Product');
        insert up;
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User u = new User(Alias = 'standt', Email='',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='');
        insert u;
        System.runAs(u) {
                PageReference pageRef         = Page.ProductUpdate;
                    ApexPages.currentPage().getParameters().put('contactId', c.Id);

You need to create an instance of your controller to test against:

ExtUserProduct ctrl=new ExtUserProduct(new ApexPages.StandardController(c));

You can then execute methods and assert the responses are as expected - for example: 

List<User_Product__c> ups=ctrl.getlstUserProduct();
System.assertNotEquals(ups, null);

Hi Bob, thanks for the help! I'm trying to create the instance of the trigger, but I'm getting a the following complie error:

Constructor not defined: [extUserProduct].<Constructor>(ApexPages.StandardController)

Any suggestions? Thanks!