You need to sign in to do that
Don't have an account?
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');
}
}
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
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.
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
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.