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

how to write test method fro apex trigger?
Hi,
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..
Code:
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; }
Thanks,
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.
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
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.
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
Message Edited by learnSF on 09-22-2008 11:44 AM
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.
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.
Can you post the test results?
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.
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.
--crop1645private class TestObjectTriggerTest
{
static TestMethod void Test0_TestInsertWithValue()
{
string test0Value = ‘PBTest 0′;
insertTestObjectWithField2Value(test0Value);
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()
{
insertTestObject();
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′);
}
}