Skip to main content
Post Closed as "Duplicate" by Adrian Larson apex
deleted 244 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

Is it possible to get some assistance on my code and test? ItIt 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. I feel like I may be at the point where a step back and another brain to tell me where I'm getting confused?

Please see below for justBelow is an example of one of the Apexmy classes and its test methods!.

I purposely included a carbon copy of the code for this instance, but zero coverage still

Have I just gottenpurposely included a carbon copy of the code for this completely wrong?

Any help is appreciatedinstance, but zero coverage still. Thank youHow can I cover this code?

Is it possible to get some assistance on my code and test? 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. I feel like I may be at the point where a step back and another brain to tell me where I'm getting confused?

Please see below for just one of the Apex classes and test methods!

I purposely included a carbon copy of the code for this instance, but zero coverage still

Have I just gotten this completely wrong?

Any help is appreciated. Thank you

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.

I purposely included a carbon copy of the code for this instance, but zero coverage still. How can I cover this code?

improved formatting
Source Link
Mark Pond
  • 23.1k
  • 2
  • 60
  • 104

global with sharing class AccountingPeriodCreationService  {

    public static List  accountingPeriod;
    public static Date currentDate;
    public static Date firstDayOfCurrentMonth;
    public static Date lastDayOfCurrentMonth;

    public static void createdAPLogic(){
        
        Set firstDaysOfMonths = new Set();
        accountingPeriod = new List (); // New list for accountingPeriod Object

    // Create a list of dates for the next 10 years

        for (Integer i = 0; i  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 accountingPeriod = Database.insert(accountingPeriod, false);
        

    } 
}
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);
        

    } 
}

private class AccountingPeriodCreationServiceTest {

     static testmethod void checkIfAlreadyExistsTest() {
         AccountingPeriodCreationService CAP = new AccountingPeriodCreationService();
        /* Pre requisites, insert Accounting Period */
        List  accountingPeriod;
        Date currentDate;
        Date firstDayOfCurrentMonth;
        Date lastDayOfCurrentMonth;
         Test.startTest();

         accountingPeriod = new List (); // New list for accountingPeriod Object
         Set firstDaysOfMonths = new Set();
         for (Integer i = 0; i  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());
            
        

     }
}
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());
            
        

     }
}

global with sharing class AccountingPeriodCreationService  {

    public static List  accountingPeriod;
    public static Date currentDate;
    public static Date firstDayOfCurrentMonth;
    public static Date lastDayOfCurrentMonth;

    public static void createdAPLogic(){
        
        Set firstDaysOfMonths = new Set();
        accountingPeriod = new List (); // New list for accountingPeriod Object

    // Create a list of dates for the next 10 years

        for (Integer i = 0; i  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 accountingPeriod = Database.insert(accountingPeriod, false);
        

    } 
}

private class AccountingPeriodCreationServiceTest {

     static testmethod void checkIfAlreadyExistsTest() {
         AccountingPeriodCreationService CAP = new AccountingPeriodCreationService();
        /* Pre requisites, insert Accounting Period */
        List  accountingPeriod;
        Date currentDate;
        Date firstDayOfCurrentMonth;
        Date lastDayOfCurrentMonth;
         Test.startTest();

         accountingPeriod = new List (); // New list for accountingPeriod Object
         Set firstDaysOfMonths = new Set();
         for (Integer i = 0; i  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());
            
        

     }
}
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);
        

    } 
}
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());
            
        

     }
}
Source Link
David Picksley
  • 465
  • 1
  • 4
  • 17

Tests passing with zero code coverage

Is it possible to get some assistance on my code and test? 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. I feel like I may be at the point where a step back and another brain to tell me where I'm getting confused?

Please see below for just one of the Apex classes and test methods!

global with sharing class AccountingPeriodCreationService  {

    public static List  accountingPeriod;
    public static Date currentDate;
    public static Date firstDayOfCurrentMonth;
    public static Date lastDayOfCurrentMonth;

    public static void createdAPLogic(){
        
        Set firstDaysOfMonths = new Set();
        accountingPeriod = new List (); // New list for accountingPeriod Object

    // Create a list of dates for the next 10 years

        for (Integer i = 0; i  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 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  accountingPeriod;
        Date currentDate;
        Date firstDayOfCurrentMonth;
        Date lastDayOfCurrentMonth;
         Test.startTest();

         accountingPeriod = new List (); // New list for accountingPeriod Object
         Set firstDaysOfMonths = new Set();
         for (Integer i = 0; i  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

Have I just gotten this completely wrong?

Any help is appreciated. Thank you