1

I despair at the moment getting the code coverage on a newly created apex class. It's a manager class without without any sObject references which I plan to use for triggers and other manager classes.

My problem is the following: I wrote an apex test class to get the code coverage for the "ScheduleManager" class. Even though the test class passes and the assertequals method succeeds (also fails if I enter a wrong assertion), the code coverage for the given class is still 0%.

The method in my manager class looks like this:

public static TimeFrame CalculateScheduleByDate(Date startDate, Date endDate, Date scheduleStart, integer periodSchedule){
        System.Debug('-->Entering ScheduleManager.CalculateScheduleByDate with parameters: StartDate=' + startDate.format() + ', EndDate=' + endDate.format() + ', ScheduleStart=' + scheduleStart.format() + ', PeriodSchedule='+ periodSchedule);

        List<TimePeriod> periods = new List<TimePeriod>();

        //Just to check: totalWeighting should be a full number at the end of the calculation.
        Decimal totalWeighting = 0;

        //-----Calculate the first Period-----//
        //Identify the start of the first period (before the start date)
        if (scheduleStart <> startDate){

            //Calculate first weighting
            decimal firstWeighting = CalculateWeight(startDate, scheduleStart, periodSchedule);

            periods.add(new TimePeriod(startDate, scheduleStart, firstWeighting));
            System.debug('Period ' + startDate.format() + ' - ' + scheduleStart.format() + ' - Weighting: ' + firstWeighting * 100 + '%');

            totalWeighting = firstWeighting;
        }

        //-----Calculate periods in between-----//
        Date currentPeriodStart = scheduleStart;
        Date currentPeriodEnd = scheduleStart.addMonths(periodSchedule);
        integer multiplier = 1;

        while (currentPeriodStart.addMonths(periodSchedule) <= endDate){
            periods.add(new TimePeriod(currentPeriodStart, currentPeriodEnd, 1));
            System.debug('Period ' + currentPeriodStart.format() + ' - ' + currentPeriodEnd.format() + ' - Weighting: 100%');

            currentPeriodStart = scheduleStart.addMonths(periodSchedule * multiplier);
            currentPeriodEnd = scheduleStart.addMonths(periodSchedule * (multiplier+1));
            multiplier = multiplier + 1;
            totalWeighting = totalWeighting + 1;
        }

        //-----Calculate last period-----//
        //Identify the start of the first period (before the start date)
        if (currentPeriodStart <> endDate){
            Date lastPeriodStart = currentPeriodStart;
            Date lastPeriodEnd = currentPeriodStart.addMonths(periodSchedule);

            //Calculate last weighting
            //Same as for the first period: Weights differ in months
            decimal lastWeighting = CalculateWeight(currentPeriodStart, endDate, periodSchedule);

            periods.add(new TimePeriod(lastPeriodStart, endDate, lastWeighting));

            totalWeighting = totalWeighting + lastWeighting;
            System.debug('Period ' + lastPeriodStart.format() + ' - ' + endDate.format() + ' - Weighting: ' + lastWeighting * 100 + '%');
        }

        System.debug('Total Periods: ' + periods.Size()); 
        System.debug('Total Weighting: ' + (totalWeighting * 100) + '%');

        //Return
        TimeFrame tf = new TimeFrame(startDate, endDate, scheduleStart, periodSchedule, totalWeighting, periods);

        return tf;
    }

My test class looks like this:

@isTest
public class ScheduleManagerTest {

    public static TestMethod void ByDate_SameStartScheduleAndEndDay(){        

        Date startDate = Date.newInstance(2015, 01, 01);
        Date scheduleStartDate = Date.newInstance(2015, 01, 01);
        Date endDate = Date.newInstance(2015, 07, 01);

        TimeFrame tf = ScheduleManager.CalculateScheduleByDate(startDate, endDate, scheduleStartDate, 1);

        System.assertEquals(6, tf.TotalWeight); 
    }
}

As you can see in the debug log, the test runs successfuly. enter image description here

But I stil have 0% code coverage: enter image description here

I already did some research, but I haven't found any similar thread for this problem. Most of the test coverage threads handly the lack of test coverage on triggers. Does anybody have an idea on this? I am happy to share the full development if it helps.

Thanks in advance!

Best regards,

Konstantin

2
  • 1
    Hi Konstantin, welcome to SF.SE. Please take a look at How do I ask a good question, then edit your question to add the code you're actually trying to test. When you post code, after pasting it in, please select it and click on the {} pre-formatted text icon so that it's legible to anyone who reads it. It's always a good practice to look at the preview pane of your post to fix any problems before posting it.
    – crmprogdev
    Commented Oct 25, 2015 at 13:41
  • Thanks for the feedback. I just added the method I am trying to test. I haven't found any similiar questions here - most of them handle problems with code coverage on triggers. I haven't any relation to any sObject in my class. Commented Oct 25, 2015 at 13:57

3 Answers 3

2

Interesting - seems like this is a bug in the developer console. I was getting an error when I tried to upload this package because I had only 50% code coverage. After adding some additional test methods I got the needed code coverage - but it still shows 0% in the developer console.

1

In Winter '16, if you run unit test classes synchronously your coverage is 0%. You need to run it asynchronously. Do to developer console and Test -> Always Run asynchronously. Your test will run asynchronously but you will get the test coverage.

0

Developer Console in Winter '16 occasionally has issues showing code coverage when the test run was started from within Developer Console. If you start the test run from Setup -> Develop -> Apex Test Execution WITH DEVELOPER CONSOLE OPEN, your test coverage should be accurately reflected.

Of course, the best way to get your for-sure test coverage number is with the "Estimate Code Coverage" link on Setup -> Develop -> Apex Classes followed by trying to upload...

You must log in to answer this question.

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