You need to sign in to do that
Don't have an account?
Jan Kopejtko 2
Testing class for the simpliest of codes
Hey guys, I've got a very simple trigger:
I wrote a test class for it:
I have two questions:
1) What am I doing wrong?
2) How come my trigger does not need to be tested and works already? I added an Account record already and the field gets populated.
Have a nice day
trigger TF on Account (before update, before insert) { for (Account a: trigger.New) { a.TestField__c = 'test'; } }
I wrote a test class for it:
@isTest public class TFTest { @isTest static void testName() { Account cat = new Account(); cat.Name = 'cat'; insert cat; System.assertEquals('test', cat.TestField__c); } }When I run the test, I get an error:
Error Message System.AssertException: Assertion Failed: Expected: test, Actual: null Stack Trace Class.TFTest.testName: line 7, column 1
I have two questions:
1) What am I doing wrong?
2) How come my trigger does not need to be tested and works already? I added an Account record already and the field gets populated.
Have a nice day
Can you please try the updated code given below.
Thanks,
Vinoth
All Answers
Can you please try the updated code given below.
Thanks,
Vinoth
public class TFTest {
@isTest static void testName() {
Account cat = new Account();
cat.Name = 'test';
insert cat;
System.assertEquals('test', cat.Name);
}
}
2)Every trigger works if written correctly. If you need to deploy a trigger into Production, you need a minimum code coverage of 75%