1

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?

0

1 Answer 1

3

This may be closed as a duplicate (we have questions under the tag to handle questions like these), but...

The big thing you're missing here is that you only gain coverage for code that is executed as part of a test method (and the things that can be covered/need coverage is generally things inside of class methods).

Your test here is calling the default constructor for AccountingPeriodCreationService, but nothing else (at least not directly).

The code you have is in the static createdAPLogic() method.

Your test isn't calling createdAPLogic(), therefore you gain no coverage for this method.

How do I write an Apex unit test? would be a good place to get some more specific (but still general) information (like you should be making assertions against the output of the code that you're testing...which could involve accessing class variables, checking return values, or querying for records inserted/updated).

Not the answer you're looking for? Browse other questions tagged .