+ Start a Discussion
Tarun SuriTarun 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
 
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
Best Answer chosen by Tarun Suri
Himanshu ParasharHimanshu Parashar
Do you have any other trigger in your org on Account object, it seems like that you have exception and no Account created from test class. Try to run following code.
 
@isTest public class AnnualTest {
@isTest
static void testme(){
Integer count=[select count() from Account];
Account a1=new Account(Name='aaa',Industry='Banking');
Account a2=new Account(name='bbb',Industry='Energy');

insert a1;
insert a2;
system.assernotequals(null,a1.id);
system.assernotequals(null,a2.id);

Integer size=[select count() from Account];
System.assertEquals(size,count+2);
Account acc=[select annualrevenue from Account where id=:a1.id];
System.assertEquals(acc.annualRevenue,5000000);
Account acc1=[select annualrevenue from Account where id=:a2.id];
System.assertNotEquals(acc1.annualRevenue,a1.AnnualRevenue);
}
}

Thanks,
Himanshu

All Answers

Himanshu ParasharHimanshu Parashar
Do you have any other trigger in your org on Account object, it seems like that you have exception and no Account created from test class. Try to run following code.
 
@isTest public class AnnualTest {
@isTest
static void testme(){
Integer count=[select count() from Account];
Account a1=new Account(Name='aaa',Industry='Banking');
Account a2=new Account(name='bbb',Industry='Energy');

insert a1;
insert a2;
system.assernotequals(null,a1.id);
system.assernotequals(null,a2.id);

Integer size=[select count() from Account];
System.assertEquals(size,count+2);
Account acc=[select annualrevenue from Account where id=:a1.id];
System.assertEquals(acc.annualRevenue,5000000);
Account acc1=[select annualrevenue from Account where id=:a2.id];
System.assertNotEquals(acc1.annualRevenue,a1.AnnualRevenue);
}
}

Thanks,
Himanshu
This was selected as the best answer
Tarun SuriTarun Suri
Himanshu Parashar
Cool its working now . the test class got the success but the problem is my trigger code coverage is 0% why it is so ?
ManojjenaManojjena
Hi Tarun,
Check your trigger is active or not ? If active try with below code once .
Incase any mandatory fields please add in the test record .
 
@isTest 
public class AccountRevenueTest {
   @isTest private static void unitTest(){
		Account acc=new Account(Name='TestAccount',Industry='Banking');
            Insert acc;
             acc=[SELECT Name FROM Account WHERE id=:acc.id];
			 System.assertEquals(acc.Name,'TestAccount');

    }
}
Let us know if it helps !!
Thanks
Manoj