I have Added Limit to the Query and in Test Class @isTest(SeeAllData=false), if(!Test.isRunningTest()) -Every Solution I found Here .Even it's not working here .So please any one can Help?
Apex Class
global class ChapterRollupBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
//return Database.getQueryLocator([select Id, Name, Type,
ChatterGroupId__c from Account where ChatterGroupId__c = null]);
return Database.getQueryLocator([select Id, Name,
Type,NumberOfPledges__c, NumberOfUndergraduates__c,
NumberOfAlumnus__c from
Account where type = 'Chapter' OR type = 'Colony' LIMIT 1]);
}
global void execute(Database.BatchableContext BC, List<Account> ChapterList)
{
for (Account Chapter : ChapterList)
{
Chapter.ACA_Number_of_Pledges__c = [SELECT Count() FROM
Association__c where Pledge_POO__c = 1 AND
Chapter__c =: Chapter.Id ];
Chapter.ACA_Number_of_Initiates__c = [SELECT Count() FROM
Association__c Where Initiate_POO__c = 1 AND
Chapter__c =: Chapter.Id ];
Chapter.ACA_Number_of_Alumni_Sans_Celestial__c = [SELECT Count() FROM
Association__c WHERE Alumni_POO__c = 1 AND
Chapter__c =: Chapter.Id ];
Chapter.of_Cases__c = [SELECT Count() FROM Case WHERE AccountId =
:Chapter.Id];
Chapter.Certs_held_by_UGs__c = [SELECT Count() FROM AdditionalInfo__c
WHERE Student__r.PreferredChapter__c = :Chapter.Id
AND Student__r.ACA_Member_Role_B__c =
'Initiate' AND
(AwardStatus__c = 'Approved' OR
AwardStatus__c = 'Recipient')];
Chapter.Of_Initiates_Registered__c = [SELECT Count() FROM User WHERE
Contact.ACA_Member_Role_B__c = 'Initiate'
AND Contact.PreferredChapter__c =: Chapter.Id];
Chapter.Of_Pledges_Registered__c = [SELECT Count() FROM User WHERE
Contact.ACA_Member_Role_B__c = 'Pledge' AND
Contact.PreferredChapter__c =:
Chapter.Id];
Chapter.Number_of_Initiates_All_Time__c = [SELECT Count() FROM
Contact WHERE AccountId =:Chapter.Id AND
InitiationDate__c != null ];
update Chapter;
}
}
global void finish(Database.BatchableContext bc)
{
if(!Test.isRunningTest())
{
//Execute the Chapter Dashboard Batch
database.executeBatch(new ChapterDashboardBatch(), 1);
}
}
}
Test Class
@isTest(SeeAllData=false)
public class TestChapterRollupBatch {
static testMethod void testMethod1()
{
try{
List<Contact> lstcontact= new List<Contact>();
for(Integer i=0 ;i <5;i++)
{
Contact con = new Contact();
con.LastName ='ContactName'+i;
lstcontact.add(con);
}
insert lstcontact;
List<Account> lstAccount= new List<Account>();
for(Integer i=0 ;i <1;i++)
{
Account acc = new Account();
acc.Name ='Name'+i;
acc.type ='Chapter';
acc.ContactName__c = lstcontact[i].LastName;
lstAccount.add(acc);
}
insert lstAccount;
Test.startTest();
ChapterRollupBatch obj = new ChapterRollupBatch();
obj.execute(null, lstAccount);
Id batchID = Database.executeBatch(obj,200);
System.abortJob(batchID);
Test.stopTest();
}
Catch(Exception e)
{
system.debug('Exception:'+e+e.getLineNumber());
}
}
}
update