You need to sign in to do that
Don't have an account?
DumasRox
Please help with test class for simple insert/update trigger
This is my first trigger. I am not sure how to do the test class.
Any help would be greatly appreciated!
trigger setAccountOwnerName on Account (before insert, before update)
{ Map<Id,String> userMap = new Map<Id, String>();
for (User u : [Select Id, Name From User]) {
userMap.put(u.Id, u.Name);
}
for (Account a : Trigger.New) {
a.Owner_Name__c = userMap.get(a.OwnerId);
}
}
You need to create a new Apex Class that performs an Account insert DML statement.
You need to configure your Account instance properties to match what your setting in the code:
For example:
@isTest
private class AccountTriggerTest
{
static testMethod void testAccountInsert()
{
Account testAccount = new Account();
testAccount.Name = 'Unit Test Name';
insert testAccount;
Account resultAccount = [Select Id, Name from Account where Id = :testAccount.Id];
system.assertEquals(testAccount.Name,resultAccount.Name);
}
}
All Answers
You need to create a new Apex Class that performs an Account insert DML statement.
You need to configure your Account instance properties to match what your setting in the code:
For example:
@isTest
private class AccountTriggerTest
{
static testMethod void testAccountInsert()
{
Account testAccount = new Account();
testAccount.Name = 'Unit Test Name';
insert testAccount;
Account resultAccount = [Select Id, Name from Account where Id = :testAccount.Id];
system.assertEquals(testAccount.Name,resultAccount.Name);
}
}
That should be enough to get you started. Their is a full section about testing apex in the Apex Developer Guide http://www.salesforce.com/us/developer/docs/apexcode/index.htm .
That should be enough to get you started. Their is a full section about testing apex in the Apex Developer Guide http://www.salesforce.com/us/developer/docs/apexcode/index.htm
This is very helpful. Thank you!