Skip to main content

For the life of me I cannot get this test class to work. It does not appear to be firing the batch class. Here is the code for the class:

global class csLastActivityBatch implements Schedulable {

global void execute(SchedulableContext SC) {

List<Account> AccountToUpdate = new List<Account>();

List<Account> acc = [Select Id,

Last_CS_Activity__c

from Account

where Type='Customer' or Type='Reseller Customer' ];

List<User> usr = [Select Id,

Department

from User

where Department='Customer Success' ];

Map<Id,Date> accountMap = new Map<Id,Date>();

for(AggregateResult ar : [Select AccountId, max(StartDateTime)

from Event

where AccountId IN: acc

and OwnerId IN: usr

and StartDateTime <= Today

group by AccountId])

{

accountMap.put(String.valueOf(ar.get('AccountId')),Date.valueOf(ar.get('expr0')));

}

for(Id parentId: accountMap.keySet())

{

AccountToUpdate.add(new Account(Id = parentId, Last_CS_Activity__c = accountMap.get(parentId)));

}

Update AccountToUpdate;

}

}

and the test class

@isTest

private class testcsLastActivityBatch {

@isTest

static void csActivityTestClass()

{

Profile pp = [SELECT Id FROM Profile WHERE Name='CS User'];

User u = new User();

u.email = 'username2099NQG@Ducoxyz.com';

u.CompanyName = 'TEST';

u.Lastname = 'Last';

u.Department = 'Customer Success';

u.Title = 'title';

u.Alias = 'alias';

u.TimeZoneSidKey = 'America/Los_Angeles';

u.EmailEncodingKey = 'UTF-8';

u.LanguageLocaleKey = 'en_US';

u.LocaleSidKey = 'en_US';

u.IsActive = true;

u.username = 'username2099NQG@Ducoxyz.com';

u.ProfileId = pp.Id;

insert u;

Account a = new Account();

a.Name = 'Test Account XYXY';

a.BillingCountry = 'United Kingdom';

a.BillingCity = 'London';

a.Type = 'Customer';

a.Segment__c = 'Bank';

a.AnnualRevenue = 10;

a.Last_CS_Activity__c = date.ValueOf('1968-09-20');

insert a;

Event t = new Event();

t.Subject = 'Test Subject';

t.ActivityDate = date.ValueOf('1968-09-22');

t.WhatId = a.id;

t.type = 'Client services Call';

t.ActivityDateTime = datetime.newInstance(1968, 9, 22, 13, 30, 0);

t.DurationInMinutes = 60;

t.OwnerId = u.Id;

insert t;

t = [SELECT id, ActivityDate, OwnerId, AccountId, WhatId, StartDateTime FROM Event WHERE id= :t.id];

Update t;

a = [SELECT id, Name, Last_CS_Activity__c, Type FROM Account WHERE id= :a.id];

Update a;

Test.startTest();

csLastActivityBatch sh1 = new csLastActivityBatch();

String sch = '0 0 2 * * ?';

String jobId = system.schedule('Test Check', sch, sh1);

CronTrigger ct = [Select id , CronExpression, TimesTriggered from CronTrigger where id = :jobId];

System.assertEquals(sch,ct.CronExpression);

System.assertEquals(0,ct.TimesTriggered);

Test.stopTest();

System.assertEquals(1,ct.TimesTriggered);

System.assertEquals(a.Last_CS_Activity__c,t.ActivityDate);

}

}

Any help on gettiong it to run would be appreciated. The class itself works perfectly in the sandbox but I cannot get the test clas to work.

8 Antworten
  1. 12. Apr. 2019, 21:15

    here is the failure: System.AssertException: Assertion Failed: Expected: 1, Actual: 0 When I comment this one out the 2nd assert fails: System.AssertException: Assertion Failed: Expected: 1968-09-20 00:00:00, Actual: 2019-04-01 00:00:00

0/9000