Test Class for Lead Reassignment


/ Published in: Java
Save to your folder(s)

This is the test class for the LeadReassignment class.


Copy this code and paste it in your HTML
  1. @isTest
  2. private class LeadReassignOnActivityHistoryTest {
  3.  
  4. static testMethod void TestLeadReassign() {
  5.  
  6. // User Id's for the two sales managers.
  7. String aId = '005A0000000i84o';
  8. String bId = '005A0000000i84z';
  9.  
  10. List <Lead> leadList = new List <Lead>();
  11. List <Task> tasks = new List <Task>();
  12.  
  13. Test.StartTest();
  14.  
  15. // Create 100 Leads and assign them to the sales manager of the opposite location.
  16. for (integer i=0; i<100; i++) {
  17. Lead l = new Lead(FirstName='LocationA',
  18. LastName='Lead'+ i,
  19. Location__c = 'Location B',
  20. Company = 'NA',
  21. OwnerId = bId);
  22. leadList.add(l);
  23.  
  24. } //close for-loop
  25.  
  26. // Create 100 more Leads and assign them to the sales manager of the opposite location.
  27. for (integer i=0; i<100; i++) {
  28. Lead l = new Lead(FirstName='LocationB',
  29. LastName='Lead' + i,
  30. Location__c = 'Location A',
  31. Company = 'NA',
  32. OwnerId = aId);
  33. leadList.add(l);
  34.  
  35. } //close for-loop
  36.  
  37. insert leadList;
  38.  
  39. List <Lead> leads = [Select ID, FirstName from Lead Where FirstName like 'Location%' AND CreatedDate = TODAY limit 200];
  40.  
  41. // Create a Task for each Lead that was just inserted. Set the date of the task so that it will set the
  42. // Last Activity Date to a date older than your protection period.
  43. for (Integer i=0; i<200; i++) {
  44. Task tsk = new Task();
  45. tsk.WhoId = leads.get(i).Id;
  46. tsk.ActivityDate = System.today() - 11;
  47. tsk.Status = 'Completed';
  48. tsk.Subject = 'Test Subject';
  49. tsk.Type = 'Call';
  50.  
  51. tasks.add(tsk);
  52.  
  53. } // close for-loop
  54.  
  55. try {
  56. insert tasks;
  57. } catch (System.DMLexception e) {
  58. System.debug('Task List not inserted: ' + e);
  59. }
  60.  
  61.  
  62. // Call the Batch Apex method.
  63. LeadReassignOnActivityHistory lr = new LeadReassignOnActivityHistory();
  64. ID batchprocessid = Database.executeBatch(lr);
  65. Test.StopTest();
  66.  
  67. AsyncApexJob async = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems from AsyncApexJob where Id = :batchprocessid];
  68. System.debug('Final results are ' + async);
  69.  
  70. System.AssertEquals(async.NumberOfErrors, 0);
  71. System.AssertEquals([Select count() from Lead Where OwnerId=:aId AND FirstName='LocationA'], 100);
  72. System.AssertEquals([Select count() from Lead Where OwnerId=:bId AND FirstName='LocationB'], 100);
  73. System.AssertEquals([Select count() from Task Where Subject = 'Test Subject'], 200);
  74.  
  75.  
  76.  
  77. } //close testmethod
  78.  
  79. } //close Class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.