It seems like literally every test I write is passing with zero code coverage org wide. I've spent the past 2 days on and off reading forums, doing trails and debugging.
Below is an example of one of my classes and its test.
global with sharing class AccountingPeriodCreationService {
public static List <AcctSeed__Accounting_Period__c> accountingPeriod;
public static Date currentDate;
public static Date firstDayOfCurrentMonth;
public static Date lastDayOfCurrentMonth;
public static void createdAPLogic(){
Set<String> firstDaysOfMonths = new Set<String>();
accountingPeriod = new List <AcctSeed__Accounting_Period__c>(); // New list for accountingPeriod Object
// Create a list of dates for the next 10 years
for (Integer i = 0; i < 120; i++){
currentDate = Date.today().toStartofMonth();
firstDayOfCurrentMonth = currentDate.addMonths(i).toStartofMonth();
String currentDateString = String.valueOf(firstDayOfCurrentMonth).removeEnd(' 00:00:00');
firstDayOfCurrentMonth = Date.valueOf(currentDateString);
firstDaysOfMonths.add(currentDateString);
}
// Check what exists already in the database
List<AcctSeed__Accounting_Period__c> checkAccountingPeriodAlreadyExist = [SELECT Id, TEMP__c, AcctSeed__Start_Date__c, AcctSeed__End_Date__c FROM AcctSeed__Accounting_Period__c where TEMP__C IN :firstDaysOfMonths];
// For the ones that do exist, remove them from the list
for (AcctSeed__Accounting_Period__c ap : checkAccountingPeriodAlreadyExist) {
firstDaysOfMonths.remove(ap.TEMP__c);
}
// For the ones left, insert into the list
for (String dt : firstDaysOfMonths){
string currName = dt.replaceAll('.{3}$','');
Date currStart = Date.valueOf(dt);
Date currEnd = Date.valueOf(dt).addMonths(1).toStartofMonth().addDays(-1);
accountingPeriod.add(
new AcctSeed__Accounting_Period__c(
Name = currName,
AcctSeed__Status__c = 'Status',
AcctSeed__Start_Date__c = currStart,
AcctSeed__End_Date__c = currEnd
)
);
}
// Insert into the database
System.debug( 'accountingPeriod: ' + accountingPeriod.size());
List<Database.SaveResult> accountingPeriod = Database.insert(accountingPeriod, false);
}
}
Now the Test Class and one method. This is mega basic, literally to insert two items into the list, query the DB and see if there are 2 entries. (Which passes)
private class AccountingPeriodCreationServiceTest {
static testmethod void checkIfAlreadyExistsTest() {
AccountingPeriodCreationService CAP = new AccountingPeriodCreationService();
/* Pre requisites, insert Accounting Period */
List <AcctSeed__Accounting_Period__c> accountingPeriod;
Date currentDate;
Date firstDayOfCurrentMonth;
Date lastDayOfCurrentMonth;
Test.startTest();
accountingPeriod = new List <AcctSeed__Accounting_Period__c>(); // New list for accountingPeriod Object
Set<String> firstDaysOfMonths = new Set<String>();
for (Integer i = 0; i < 2; i++){
currentDate = Date.today().toStartofMonth();
firstDayOfCurrentMonth = currentDate.addMonths(i).toStartofMonth();
String currentDateString = String.valueOf(firstDayOfCurrentMonth).removeEnd(' 00:00:00');
firstDayOfCurrentMonth = Date.valueOf(currentDateString);
firstDaysOfMonths.add(currentDateString);
}
for (String dt : firstDaysOfMonths){
//System.debug(firstDaysOfMonths);
//System.debug(dt);
//System.debug(Date.valueOf(dt));
//System.debug(Date.valueOf(dt).addMonths(1).toStartofMonth().addDays(-1));
//System.debug(dt.replaceAll('.{3}$',''));
string currName = dt.replaceAll('.{3}$','');
Date currStart = Date.valueOf(dt);
Date currEnd = Date.valueOf(dt).addMonths(1).toStartofMonth().addDays(-1);
accountingPeriod.add(
new AcctSeed__Accounting_Period__c(
Name = currName,
AcctSeed__Status__c = 'Status',
AcctSeed__Start_Date__c = currStart,
AcctSeed__End_Date__c = currEnd
)
);
}
Database.insert(accountingPeriod, false);
Test.stopTest();
List<AcctSeed__Accounting_Period__c> InsertResult = [SELECT Id, TEMP__c, AcctSeed__Start_Date__c, AcctSeed__End_Date__c FROM AcctSeed__Accounting_Period__c where TEMP__C IN :firstDaysOfMonths];
System.assertEquals(2, InsertResult.size());
}
}
I purposely included a carbon copy of the code for this instance, but zero coverage still. How can I cover this code?