+ Start a Discussion

how to write test method fro apex trigger?

I worte below apext triiger but have no idea how to write test case for this tigger. I tried to search around but didn't get idea..
trigger createRateIncreaseChange on Account (before update) {

    List<Rate_Increase_Tracker__c> rateIncrease = new List<Rate_Increase_Tracker__c>();
    for (Integer i = 0; i < Trigger.new.size(); i++) {
     if ( (Trigger.old[i].Ad_Package_Order_Activated_Date__c != Trigger.new[i].Ad_Package_Order_Activated_Date__c)) {

            rateIncrease.add(new Rate_Increase_Tracker__c(New_Contract_Term__c = '1', Account__c = Trigger.old[i].id));
     insert rateIncrease;

First check out the Apex docs on testMethods, there is a great example to follow.

Then all you really need to do is test the update of an Account to fire your trigger.

In your testMethod make sure you insert an account, then update it, then test with assert statements
to see if your business logic is supported.

You should get full coverage if you just test the update of an account and change the fields you are
processing in your trigger.


Thanks for Reply,

I have seen that doc before ,but it seems all test method is related with class. I want to write test method inside trigger only.can we write like that?

if yes,where I have to put code inside trigger? just after insert list statement? It would be great if you publish some sample test cases for only trigger.

you can't write your tests in the trigger.
Create a class and write you tests in the class.
You can deploy the class with your trigger.

Message Edited by mikef on 09-22-2008 10:45 AM
Thanks for reply,
do you mean to say I have change code of trigger and transfer all logic to class instead of writing inside trigger and write test cases in class?
or without changing trigger just create class and write test method for trigger? if yes, I am bit confuse how this class will be invoke while fire trigger?
It would be great if you put some lights on it.

Message Edited by learnSF on 09-22-2008 11:44 AM
just write the tests in a class.

to fire the trigger you insert and/or update records.

Please read the apex docs on this subject there are lots of examples.

Thanks a lots mikef,

I will test case now.


Thanks mikef,

I wrote test case and its worked out for me.


Thanks for your help.

Jeff C.ax327Jeff C.ax327
I just don't see it guys... no matter how many different test methods I write for my triggers, the Force.com IDE gives me "0% coverage" when I run tests.  The apex language reference documentation doesn't seem to specifically address writing testMethod for triggers.  Is there anything special I must do to indicate which trigger my testMethod is for?  My test code inserts a record, makes a System.assertEquals call, and then deletes the record and makes another System.assertEquals call... both calls should cover what my trigger does.  Help?

Test from the UI and see what % you get. Go to your test class in setup, and there should be a button on the class view page called Run Tests.

Post the results from that button.
Jeff C.ax327Jeff C.ax327
Hi Mike, thanks for your reply.
When I click Re-Run from my test class, it shows the following:
- Failures & Warnings
---- Contact_BeforeDeleteTrigger
---------- Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
My test method definitely tests the logic in the trigger... I don't get it.
try re-compiling your trigger, edit save.

Can you post the test results?
Jeff C.ax327Jeff C.ax327
Hi Mike,
I tried editing and saving both the trigger and the test code and still ended up with the same results that I posted before.  However, I changed the logging level from None to Verbose, and I discovered that my test method wasn't being executed at all.  There was, however, one other test method in a utility class of mine that was executing.  So, I moved my trigger test method into the utility class and now it executes and I get 100% coverage for my trigger.
This is strange to me... what would keep my testMethod from executing in a stand-alone class?  I didn't change anything about the method when I moved it into the utility class.  After some research, I tried adding the @isTest annotation to the top of the original class (which necessitated making the class private), but my test code still did not run.  Any ideas?
I would like to make a stand-alone test class containing testMethods for all of my triggers.
was your original class private? Or did you have any class security?
Jeff C.ax327Jeff C.ax327
Hi Mike,
The original class was initially public... after checking the security settings, both the utility class and the testMethods class are given Administrator privileges only.  For grins, I granted access to all profiles for both the utility class and the testMethods class, but it hasn't changed the behavior -- no test methods in my testMethods class get executed.... arg...
Jeff C.ax327Jeff C.ax327

Alright, not sure what's so magical about this one utility class that I have, but it's the only place I can put any test methods.  If I put a test method in any of my other classes, they don't get evaluated when running tests.  If I cut and paste the test method (unchanged) into my utility class (it's name is indeed "utility"), then the test method is executed when running tests and I get the necessary code coverage.

Not sure why the Force.com IDE has picked this one class as its favorite, but at least I'm not stuck anymore.  At some point I'll need to get past it so I can better organize my tests.  Perhaps I should delete and recreate my Force.com project from the Salesforce server?  I need to deploy to Production first, then I'll give that a try.

Jeff C.ax327Jeff C.ax327
OH... <dunce cap on>
I was right-clicking in the editor area and choosing Force.com \ Run Tests
Turns out this will only run tests in the file you are clicking on.
Even if you switch to a different tab, showing a different class, the Re-Run button still only runs tests in the original file.
To run all tests in all classes you must right-click the "classes" node in the Package Explorer and choose Force.com \ Run Tests
Geez... took me long enough.
Jeff C -- Thank you so much.  I was wondering for some days now why when I tested in the Eclipse Force.com IDE a single class, the Apex Test Code Runner would report that my other classes/triggers had 0% coverage.

Now that (thanks to you) I know one:

  • Right clicks on Classes folder and Force.com | Run tests in order to run all classes with test methods in the folder
  • Right clicks on specific class to Force.com | Run tests to run just the test methods in that class.  In this case, one gets spurious red 'errors and warnings' about other triggers/classes in the Classes and Triggers folders that weren't executed.  The test coverage stats are 'erring' on the side of presenting you information about triggers and classes that it thinks you should have called through class when in fact you have these other classes/triggers as independent code units, unrelated to class
shraddha kawaneshraddha kawane
private class TestObjectTriggerTest

static TestMethod void Test0_TestInsertWithValue()
string test0Value = ‘PBTest 0′;

TestObject__c testObj = [Select Name, Field1__c, Field2__c from TestObject__c where Name = :test0Value];

System.assertNotEquals(testObj, null, ‘Test 0 object was null and not inserted correctly’);

System.assertEquals(testObj.Field1__c, testObj.Field2__c, ‘Field1 and Field2 not equals in test 0′);

static TestMethod void Test1_TestInsertWithoutValue()

TestObject__c testObj = [Select Name, Field1__c, Field2__c from TestObject__c where Name = 'PBTestValue'];

System.assertNotEquals(testObj, null, ‘Test 1 object was null and not inserted correctly’);

System.assertEquals(testObj.Field1__c, testObj.Field2__c, ‘Field1 and Field2 not equals in test 0′);