You need to sign in to do that
Don't have an account?

Testing an Apex Trigger
I created some very basic Apex triggers a while a go, that populate fields according to values in other fields. I am not a developer so I kept the code as basic as possible.
They all work fine.
However I have to occasionally update some of the values. Up until now I have always done this and redeployed the trigger to production. I am now getting an error "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required'
I've looked at some of the APEX documentation to see how to create a test, but I am having difficulty working out what I am meant to do. Up until now I have only created triggers and not classes.
I am not sure how to go about creating a test class for a trigger and don't understand why it has functioned up until now if this was necessary.
Any help appreciated.
Regards
KF
Thanks for this. After much playing around, I have got test classes to work.
All Answers
Hi,
For any trigger or class to upload to the production environment you will need to have "Test Method" and it should cover 75% of your code, otherwise you will not be able to deploy that code to the production.
For more information about "Test Methods" and why we require them see the article.
http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods
If you are not a developer and want to write the "Test Method" you can paste your code here and I can help you to write the Test class for the same.
If its not possible you can refer the article and then you can try to write the Test Class.
Hope you will learn to write the Test Method.
Thanks,
Mandar.
Hi Kulkarni,
Many thanks for the reply. I will read the post on the wiki. Would I need to write a Test Method for every trigger or is there some sort of catch all option?
An example of a trigger I need to test is:
trigger BillingCurrency on Account (before insert)
{
for (Account a : Trigger.new)
{
if(a.Market__c== 'ARE' || a.Market__c== 'GBR' || a.Market__c== 'RUS' ){a.CurrencyIsoCode = 'GBP' ;}
else if(a.Market__c== 'BEL' || a.Market__c== 'DEU' || a.Market__c== 'ESP'|| a.Market__c== 'FIN'|| a.Market__c== 'FRA'|| a.Market__c== 'GRC'|| a.Market__c== 'IRL'|| a.Market__c== 'ITA'|| a.Market__c== 'LUX'|| a.Market__c== 'NLD'|| a.Market__c== 'PRT'){a.CurrencyIsoCode = 'EUR' ;}
else if (a.Market__c== 'CAN'){a.CurrencyIsoCode = 'CAD' ;}
else if(a.Market__c== 'CHE'){a.CurrencyIsoCode = 'CHF' ;}
else if(a.Market__c== 'CZE'){a.CurrencyIsoCode = 'CZK' ;}
else if(a.Market__c== 'DNK'){a.CurrencyIsoCode = 'DKK' ;}
else if(a.Market__c== 'NOR'){a.CurrencyIsoCode = 'NOK' ;}
else if (a.Market__c== 'SWE'){a.CurrencyIsoCode = 'SEK' ;}
else if(a.Market__c== 'USA'){a.CurrencyIsoCode = 'USD' ;}
else {a.CurrencyIsoCode = 'GBP' ;}
}
}
Regards
KF
In order to test the trigger you need to insert records that will cause the trigger to fire. You need to create a class to do this. Because this is a test (note the static testMethod) no data is actually committed to the database. Below you will see that I create an account and set the Market to GBR, then I insert the account. Once inserted, I test to see if the trigger set the value of Iso code appropriately. I then continue to do that to ensure that all lines get tested.
Good luck!
Thanks for this. After much playing around, I have got test classes to work.