You need to sign in to do that
Don't have an account?
Tamurth
Urgent - Need Help - Code coverage test class
Dear All,
Could you help by providing a test class for my trigger and APEX classes ?
I can't get any coverage when i run my test class.
Trigger:
trigger UserCreationBeforeUpdate on UserCreationDetail__c (before update) { System.Debug('## >>> User before update <<< run by ' + UserInfo.getName()); /********************************************************************************************* AP41UserCreation Create/Update . Designed for VF40_UserRequestProcess **********************************************************************************************/ if(PAD.canTrigger('AP64')){ System.Debug('## AP64UserProcess begin prepare'); List<UserCreationDetail__c> ap64UserCreation = new List<UserCreationDetail__c>(); //List<UserCreationDetail__c> ap64UserUpdate = new List<UserCreationDetail__c>(); Set<Id> ap64UserUpdate = new Set<Id>(); for(UserCreationDetail__c usr: Trigger.new) { if (usr.Status__c != Trigger.OldMap.get(usr.id).Status__c && usr.Status__c == 'Implemented') { if (usr.Type__c == 'Creation') { ap64UserCreation.add(usr); } else { //ap64UserUpdate.add(usr); ap64UserUpdate.add(usr.id); } } } System.Debug('## AP64UserProcess end prepare'); if(ap64UserCreation.size() > 0) AP64UserManagementProcess.createUser(ap64UserCreation); if(ap64UserUpdate.size() > 0) AP64UserManagementProcess.updateUser(ap64UserUpdate); System.Debug('## >>> User Creation before update : END <<<'); } }
APEX:
public class AP64UserManagementProcess { //static List<User> usersToUpdate = new List<User>(); public static void createUser(List<UserCreationDetail__c> users) { List<User> usersToInsert = new List<User>(); try { for (UserCreationDetail__c userRequest: users) { User user = new User(); user.EmployeeNumber = userRequest.EmployeeNumber__c ; user.FirstName = userRequest.FirstName__c ; user.LastName = userRequest.LastName__c ; user.Email = userRequest.Email__c ; user.username = userRequest.username__c ; user.UserRoleId = userRequest.RoleId__c ; user.ProfileId = userRequest.ProfileId__c ; user.TechManagerId__c = userRequest.Manager__c ; user.Title = userRequest.Title__c ; user.CompanyName = userRequest.CompanyName__c ; user.Department = userRequest.Department__c ; user.Division = userRequest.Division__c ; user.EmailEncodingKey = userRequest.EmailEncoding__c ; user.userCountry__c = userRequest.userMainCountry__c ; user.userArea__c = userRequest.userMainArea__c ; user.ISORegion__c = userRequest.userMainRegion__c ; user.userMainCountrySubArea__c = userRequest.userMainCountrySubArea__c ; user.userSubRegion__c = userRequest.userMainSubRegion__c ; user.OtherRegions__c = userRequest.OtherRegions__c ; user.KeyuserType__c = userRequest.KeyuserType__c ; user.Champion__c = userRequest.Champion__c ; user.userProductLine__c = userRequest.userMainProductLine__c ; user.userUnit__c = userRequest.userMainUnit__c ; user.FinancialUnitId__c = userRequest.FinancialUnitId__c ; user.eLearning__c = userRequest.eLearning__c ; user.OtherProductLines__c = userRequest.OtherProductLines__c ; user.TimeZoneSidKey = userRequest.TimeZone__c ; user.LocaleSidKey = userRequest.Language__c ; user.LanguageLocaleKey = userRequest.Locale__c ; user.CurrencyIsoCode = userRequest.Currency__c ; user.CarveOutComments__c = userRequest.Comments__c ; user.Alias = userRequest.Alias__c ; user.CommunityNickname = userRequest.CommunityNickname__c ; user.UserPermissionsMobileUser = false; usersToInsert.add(user); } //Create users insert usersToInsert; //RESET PASSWORD for (User u: usersToInsert) System.resetPassword(u.Id, true); } catch (DmlException e) { system.debug(Logginglevel.ERROR, e); } } @future public static void updateUser(Set<Id> userDetail) { try { for (Id userId : userDetail) { UserCreationDetail__c userRequest = [SELECT Email__c,EmployeeNumber__c,FirstName__c,LastName__c,RoleId__c,ProfileId__c, Manager__c,Title__c,CompanyName__c,Department__c,Division__c,EmailEncoding__c,userMainCountry__c, userMainArea__c,userMainRegion__c,userMainCountrySubArea__c,userMainSubRegion__c,OtherRegions__c, KeyuserType__c,Champion__c,userMainProductLine__c,userMainUnit__c,FinancialUnitId__c,eLearning__c, OtherProductLines__c,TimeZone__c,Language__c,Locale__c,Currency__c,Comments__c,Alias__c,CommunityNickname__c FROM UserCreationDetail__c WHERE Id = :userId]; User user = [SELECT Id FROM User WHERE Email = :userRequest.Email__c LIMIT 1]; user.EmployeeNumber = userRequest.EmployeeNumber__c ; user.FirstName = userRequest.FirstName__c ; user.LastName = userRequest.LastName__c ; //user.Email = userRequest.Email__c ; //user.username = userRequest.username__c ; user.UserRoleId = userRequest.RoleId__c ; user.ProfileId = userRequest.ProfileId__c ; user.TechManagerId__c = userRequest.Manager__c ; user.ManagerId = userRequest.Manager__c ; user.Title = userRequest.Title__c ; user.CompanyName = userRequest.CompanyName__c ; user.Department = userRequest.Department__c ; user.Division = userRequest.Division__c ; user.EmailEncodingKey = userRequest.EmailEncoding__c ; user.userCountry__c = userRequest.userMainCountry__c ; user.userArea__c = userRequest.userMainArea__c ; user.ISORegion__c = userRequest.userMainRegion__c ; user.userMainCountrySubArea__c = userRequest.userMainCountrySubArea__c ; user.userSubRegion__c = userRequest.userMainSubRegion__c ; user.OtherRegions__c = userRequest.OtherRegions__c ; user.KeyuserType__c = userRequest.KeyuserType__c ; user.Champion__c = userRequest.Champion__c ; user.userProductLine__c = userRequest.userMainProductLine__c ; user.userUnit__c = userRequest.userMainUnit__c ; user.FinancialUnitId__c = userRequest.FinancialUnitId__c ; user.eLearning__c = userRequest.eLearning__c ; user.OtherProductLines__c = userRequest.OtherProductLines__c ; user.TimeZoneSidKey = userRequest.TimeZone__c ; user.LocaleSidKey = userRequest.Language__c ; user.LanguageLocaleKey = userRequest.Locale__c ; user.CurrencyIsoCode = userRequest.Currency__c ; user.CarveOutComments__c = userRequest.Comments__c ; user.Alias = userRequest.Alias__c ; user.CommunityNickname = userRequest.CommunityNickname__c ; user.IsActive = true; update user; } //RESET PASSWORD /*for (User u: usersToUpdate) System.resetPassword(u.Id, true);*/ } catch (DmlException e) { system.debug(Logginglevel.ERROR, e); } } }
Thank's for help,
All the best,
Anzar.
Anzar - I don't see any test class posted, assumiing you wrote one, it needs to do the following:
1. insert 1+ UserCreationDetail__c records
2. Update 1+ UserCreationDetail__c records
3. Wrap the insert within a Test.startTest() and Test.stopTest() method pair - this will cause the future emthods to execute
Should you need more assistance, refer to the Apex Developers Guide, section 'Testing Apex' | Testing Example
Hi Eric,
Thank you for helping.
Here is my test class, only 16% code coverage
All the best,
Anzar.
Hi,
Any help, please ?
All the best,
Anzar.
Anzar --
The way you go about debugging the test code coverage issue is as follows:
1. See what statements aren't being executed - you can get this from either the Eclipse IDE (if that is how you are running tests) or from the Apex Classes | <select class> | Run Tests command in the Force.com UI
2. Then ascertain why in your code you might not be executing those statements.
3. If #2 isn't obvious, add more System.debug(..) statements to expose what might be happening
I believe PAD class is provided by Salesforce. They should be having some trick to do the test coverage inside if(PAD.canTrigger(..)).
Please let me know if you found any solution for this.