You need to sign in to do that
Don't have an account?
Tarun Suri
getting an error while writing a test class for a simple trigger
i am trying to write a test class on a simple trigger below but getting error
======================================
============================================
TestRun result = Fail
TestRun Error = System.AssertException: Assertion Failed: Expected: 0, Actual: 2
TestRun Stack trace = Class.AnnualTest.testme: line 14, column 1
1. trigger accountrevenue on Account (before insert) { 2. list<account> acc=trigger.new; 3. for(account accs:acc){ 4. if(accs.industry=='Banking') 5. accs.annualRevenue=5000000; 6. } 7. }
======================================
1. @isTest public class AnnualTest { 2. @isTest 3. static void testme(){ 4. Integer count=[select count() from Account]; 5. Account a1=new Account(Name='aaa',Industry='Banking'); 6. Account a2=new Account(name='bbb',Industry='Energy'); 7. try{ 8. insert a1; 9. insert a2; 10. } catch(Exception e) { 11. System.debug(e); 12. } 13. Integer size=[select count() from Account]; 14. System.assertEquals(size,count+2); 15. Account acc=[select annualrevenue from Account where id=:a1.id]; 16. System.assertEquals(acc.annualRevenue,5000000); 17. Account acc1=[select annualrevenue from Account where id=:a2.id]; 18. System.assertNotEquals(acc1.annualRevenue,a1.AnnualRevenue); 19. } 20. }
============================================
TestRun result = Fail
TestRun Error = System.AssertException: Assertion Failed: Expected: 0, Actual: 2
TestRun Stack trace = Class.AnnualTest.testme: line 14, column 1
Thanks,
Himanshu
All Answers
Thanks,
Himanshu
Cool its working now . the test class got the success but the problem is my trigger code coverage is 0% why it is so ?
Check your trigger is active or not ? If active try with below code once .
Incase any mandatory fields please add in the test record .
Let us know if it helps !!
Thanks
Manoj