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
KirstyFranceKirstyFrance 

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

Best Answer chosen by Admin (Salesforce Developers) 
KirstyFranceKirstyFrance

Thanks for this. After much playing around, I have got test classes to work.

All Answers

MandyKoolMandyKool

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.

KirstyFranceKirstyFrance

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

kyle.tkyle.t

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.

 

 

@isTest
private class testBillingCurrencyTrigger {

    static testMethod void myUnitTest() {
		//Create an account to insert with Market = GBR
		Account acct1 = new Account(Name='Test Account', Market__c = 'GBR');
		insert acct1;
		//Test to see that the trigger fired and changed the CurrencyIsocode go GBP
		System.assertEquals('GBP',acct1.CurrencyIsoCode);
		//Set Market to ESP and insert again. keep doing this to ensure we test all lines
		Account acct2 = new Account(Name='Test Account', Market__c = 'ESP');
		insert acct2;
		System.assertEquals('EUR',acct2.CurrencyIsoCode);
		Account acct3 = new Account(Name='Test Account', Market__c = 'CAN');
		insert acct3;
		System.assertEquals('CAD',acct3.CurrencyIsoCode);
		Account acct4 = new Account(Name='Test Account', Market__c = 'CHE');
		insert acct4;
		System.assertEquals('CHF',acct4.CurrencyIsoCode);
		Account acct5 = new Account(Name='Test Account', Market__c = 'CZE');
		insert acct5;
		System.assertEquals('CZK',acct5.CurrencyIsoCode);
		Account acct6 = new Account(Name='Test Account', Market__c = 'DNK');
		insert acct6;
		System.assertEquals('DKK',acct6.CurrencyIsoCode);
		Account acct7 = new Account(Name='Test Account', Market__c = 'NOR');
		insert acct7;
		System.assertEquals('NOK',acct7.CurrencyIsoCode);
		Account acct8 = new Account(Name='Test Account', Market__c = 'SWE');
		insert acct8;
		System.assertEquals('SEK',acct8.CurrencyIsoCode);
		Account acct9 = new Account(Name='Test Account', Market__c = 'USA');
		insert acct9;
		System.assertEquals('USD',acct9.CurrencyIsoCode);
		Account acct10 = new Account(Name='Test Account', Market__c = 'AAA');
		insert acct10;
		System.assertEquals('GBP',acct10.CurrencyIsoCode);
	}
}

 

Good luck!

 

KirstyFranceKirstyFrance

Thanks for this. After much playing around, I have got test classes to work.

This was selected as the best answer