I have written a batch class to mark a certain number of an object in a date range to be marked for review, and it is working when called on from a visualforce page, but it does not work when called in a test. Can anyone help me figure out what I'm doing wrong?
Batch class is:
global class batchAssignAudit implements Database.Batchable<sObject> {
global Id auditUser{get; set;}
global Date startDate{get; set;}
global Date endDate{get; set;}
global Integer percentageToCheck {get; set;}
global Set<Object__c> objectsToAudit = new Set<Object__c>();
global List<Object__c> objectsToAuditList = new List<Object__c>();
global String query = 'SELECT ID, Needs_Audit__c FROM Object__c WHERE Status__c = \'Complete\' AND Captured_By__c = null AND Closed_Date_and_Time__c >= :startDate AND Closed_Date_and_Time__c <= :endDate';
global batchAssignAudit() {
}
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Object__c> scope){
while(objectsToAudit.size() < Math.floor(scope.size() * (Double.valueOf(percentageToCheck)/100))) {
System.debug('while loop engaged');
objectsToAudit.add(scope[Integer.valueof((Math.random() * scope.size()))]);
}
for(Object__c object : objectsToAudit){
System.debug('for loop engaged');
object.Captured_By__c = auditUser;
object.Needs_Audit__c = true;
objectsToAuditList.add(object);
}
System.debug(objectsToAuditList.size());
update objectsToAuditList;
}
global void finish(Database.BatchableContext bc){
}
}
Test class is
@isTest
private class batchAssignAuditTest {
@isTest static void batchAssignAuditTest() {
List<Object__c> objectList = new List<Object__c>();
User regUser = new User(
//constructing user code
);
insert regUser;
Account acc = new Account(
Name = 'TestAccount',
Phone = '7755559999'
);
insert acc;
File__c file = new File__c(Account__c = acc.Id,
First_Name__c = 'test',
Last_Name__c = 'name2',
Affix__c = 'jr',
SSN__c = '123456789'
);
insert file;
for(Integer i = 0; i < 100; i++) {
Object__c v1 = new Object__c(File__c = file.Id,
Status__c = 'Complete',
Closed_Date_and_Time__c = Date.today()
);
objectList.add(
v1
);
}
insert objectList;
Integer percentage = 10;
Date startDate = Date.today().addDays(-5);
Date endDate = Date.today().addDays(5);
Integer batchSize = 1;
batchAssignAudit audit = new batchAssignAudit();
audit.auditUser = regUser.Id;
audit.startDate = startDate;
audit.endDate = endDate;
audit.percentageToCheck = percentage;
Test.startTest();
if(Test.isRunningTest()) {
Database.executeBatch(audit);
}
Test.stopTest();
List<Object__c> testList = [SELECT ID FROM Object__c WHERE Needs_Audit__c = true];
System.assertEquals(10, testList.size());
}
}
Edit:
I should add the error I am getting when I run the test is
System.AssertException: Assertion Failed: Expected: 10, Actual: 0