+ Start a Discussion
jlimjlim 

Help with writing simple unit test

I'm really new in this and would appreciate any pointers. I read through examples but still not sure how to proceed. TIA.

 

 

/*** Controller ***/
public class MyControllerExtension {

    private final Opportunity oppor;
   

    public myControllerExtension (ApexPages.StandardController stdController) {
        Id id = ApexPages.currentPage().getParameters().get('Id');
       
        oppor = (id == null) ? new Opportunity() :
                [
                 SELECT Id, StageName, Account_Region__c                

                 FROM Opportunity
                 WHERE Id = :id
                ];
    }

 

    public Opportunity getOpportunity() {

        return oppor;

    }

 

   
    public String getButtonState() {
 
        if(oppor.Account_Region__c == 'UK') {
            if(oppor.StageName == 'Closed Won') {
               return 'False';
            } else {
              return 'True';
            }
        } else {
            return 'True';
        }
    }   

}

 

/*** TEST ***/

@isTest
private class TestMyControllerExtension {
  
    static testMethod void TestMyControllerExtension() {
       
        ApexPages.StandardController stdController;
        MyControllerExtension mce = new MyControllerExtension(stdController);
       
        Opportunity oppor = mce.getOpportunity();
 
        oppor.StageName = 'Closed Won';
        oppor.Account.Region__c = 'UK';
         
    
        System.assertEquals(mce.getButtonState(),'False');

    }

}

 

 

Message Edited by jlim on 07-27-2009 02:49 PM
Siddhesh KabeSiddhesh Kabe
What exactly is the problem??? :smileysurprised:
KrishnadasKrishnadas

Hi
 
In the Unit test you missed a intiating the value for the variable Id in the class that is taken from the 
page URL, secondly the point at which you are getting the error is where you are trying to provide value for oppor.Account.Region__c. This should have been oppor.Account_Region__c.
 
I am including an example of an easiest unit test for your code

 

@isTest
private class TestMyControllerExtension {
    static testMethod void TestMyControllerExtension() {
/*  Create a Opportunity record with the values to be used in the test class
    this kinds of substitutes what the users would enter in the UI */
        Opportunity testOppor = new Opportunity(Name = 'TestOppor', StageName = 'Closed Won', Account_Region__c = 'UK', CloseDate = system.today());
        insert testOppor;
/*  Set the value for Id that you are attempting to get from the Page URL */
        apexPages.currentPage().getParameters().put('Id', testOppor.Id);
        apexPages.standardController stdController = new apexPages.standardController(new Opportunity());
        MyControllerExtension mce = new MyControllerExtension(stdController);
/*  evaluate the get methods in the class */
        Opportunity oppor = mce.getOpportunity();
        string buttonState = mce.getbuttonState();
/*  provide the values to the variables based on which the condition is 
    evaluated */
        oppor.StageName = testOppor.StageName;
        oppor.Account_Region__c = testOppor.Account_Region__c;
        oppor.StageName = 'Closed Lost';
        buttonState = mce.getbuttonState();
        oppor.Account_Region__c = 'USA';
        buttonState = mce.getbuttonState();
    }
}

 

Hope this helps you.
 
Thanks
Krishnadas
Steadfast Global
www.steadfastglobal.com 

 

jlimjlim

Krishnadas - thanks for your example. I manage to get much further along now. The reason why I'm using oppor.Account.Region__c is because that field maps to the Account.Region__c and is automatically populated. If I reference by oppor.Account_Region__c then I get a "non writetable error".

 

Here's a question in general and I would like to know the correct way of doing. After setting the following:

 

 

oppor.StageName = 'Closed Won';

 

Should I also do

 

update oppor;

 

When I run the test now, I'm getting 100%. But if I do "update oppor;" then the validation rule kicks in and it forces me to enter a dependent field (reason).

 

What is the correct way? Thanks.

KrishnadasKrishnadas
I have assumed that the unit tests are for evaluating the logic in Apex Class and triggers that we write. You need not do an update for the current scenario.
You would do an update in the test class if you are trying to do a test coverage for an after update trigger.
 
Thanks
Krishnadas
Steadfast Global
www.steadfastglobal.com 
Siddhesh KabeSiddhesh Kabe

Update trigger will be used only when there is a logic of updation in the code. Try running the test in Eclipse IDE using update, you will have a higher result.

 

Always write test classes based on the scenario for which you are writing the logic. If you wish to test Update and the above code in one test class, you can update it.

 

P.s. Any data manipulation done in test classes does not reflect in main org.

 

Hope it helps,

Siddhesh Kabe

http://sidoscope.blogspot.com

Message Edited by Siddhesh Kabe on 07-28-2009 10:33 PM
jlimjlim

Thanks for the replies and pointers. In my particular case, there's no need for me to do the update as all I'm doing in evaluating what the current values are (Region__c and StageName). But it is good to know for future code development.

 

When I did the update, I was "forced" to create the corresponding Product and Pricebook as we need a product in the opportunity due to validation rule. So, it is good to learn how to create those as well eventhough I didn't need it if I hadn't call the update code.