1

The purpose of the controller below is to return the value of the one__c field on the Acc_Plan__c object, and then pass it into the getIsOneTrue() method.

The controller works fine, but I have to write a unit test for it. I have never written a unit test before and the whole concept is new to me.

I'm wondering as my controller is pretty basic, is there any sort of 'standard' unit test I could adapt to fit my purpose or could someone perhaps show me or direct me towards a very basic example which would point me in the right direction?

public with sharing class MyController {

    private ApexPages.StandardController standardController;

    public String AccPlanId { get; set; } 
    public Boolean one { get; set; }    
    public My_Account__c acc;

    public MyController (ApexPages.StandardController standardController) {        

        this.standardController = standardController;

        String Id = (standardController.getRecord()).id;

        acc = [Select Acc_Plan__r.Id FROM My_Account__c where id=:Id]; 

        AccPlanId = acc.Acc_Plan__r.Id;

        String query = 'SELECT one__c FROM Acc_Plan__c WHERE ID = :AccPlanId LIMIT 1';

        Acc_Plan__c accPlan = database.query(query);

        one = accPlan.one__c;
    }

    public Boolean getIsOneTrue() {
        return one;
    }

}

Any advice is greatly appreciated, cheers.

UPDATE

Taking crmprogdev's advice and after a few hours trial and error, I came up with this. 100% code coverage.

Hopefully this will help someone who might find themselves in my position at some point.

@isTest
private class MyControllerTest {

    @isTest 
    static void testController() {            

        Acc_Plan__c accPlan = new Acc_Plan__c(Name = 'Test acc plan', one__c = true);
        insert accPlan;

        My_Account__c acc = new My_Account__c(Name='Test acc', Acc_Plan__c = accPlan.Id);
        insert acc;

        Test.startTest();

        ApexPages.StandardController standardController = new ApexPages.StandardController(acc);
        MyController myControllerTest = new MyController(standardController);

        System.assertNotEquals(null, myControllerTest, 'Null result returned from myControllerTest(ApexPages.StandardController)');
        System.assertEquals(accPlan.Id, tamSummaryTabDisable.AccPlanId, 'Acc Plan not set correctly');

        Test.stopTest(); 
    }

    @isTest
    static void testGets() {

        Acc_Plan__c accPlan = new Acc_Plan__c(Name = 'Test acc plan', one__c = true);
        insert accPlan;

        My_Account__c acc = new My_Account__c(Name='Test acc', Acc_Plan__c = accPlan.Id);
        insert acc;

        ApexPages.StandardController standardController = new ApexPages.StandardController(acc);
        MyController myControllerTest = new MyController(standardController);    

        System.assertEquals(true, myControllerTest.getIsOneTrue());  
    } 

}
1

Take a look at How should I build test methods for Visualforce Controller Extensions. I think you'll find the guidance you seek in the answers to that question.

Also be sure to add system.asserts at various points in your test class to make certain its doing what you expect it to do!

  • Took your advice and worked at it until I came up with the above. Tests pass and 100% code coverage. Could you possibly have a look and tell me if anything looks redundant or out of place? – Daft Aug 29 '14 at 8:49
  • I would suggest adding your test class to your original question and marking @crmprogdev's post as the answer. – Davin Casey Aug 29 '14 at 8:56
  • 1
    @DavinC will do – Daft Aug 29 '14 at 9:07
  • 1
    Looks good to me. Well done. – crmprogdev Aug 29 '14 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.