function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AngiB.ax1285AngiB.ax1285 

A Test class that to my Newbie Eyes doesn't seem to test much of anything (but gives 60% coverage)

I have a test class for a fairly large class that can be run either via a button or as a scheduled task

(not related to my previous post of today about the || operator – different class)

 

Whether the code is run via a button or scheduled it does NOT have any external input – it just evaluates data in various source custom objects and creates one or more records in a set of 3 different destination custom objects (a parent, child and grandchild structure based on the source objects).

 

I did not write ANY of the code involved – someone else was contracted to do that. But I am attempting to learn by example and this example brings various questions to my mind. 

 

So my test class is….

 

@isTest
private class Test_ScheduleClass_DepCalculForCalPeriod {

    static testMethod void myUnitTest() {
ScheduleClass_DepCalculationForCalPeriod objDepForCalPeriod = new ScheduleClass_DepCalculationForCalPeriod();
        objDepForCalPeriod.execute(null);
        system.assert(true);
    }
}

 

The class being tested – called ScheduleClass_DepCalculationForCalPeriod() contains:

 

global with sharing class ScheduleClass_DepCalculationForCalPeriod implements Schedulable
{
    global void execute(SchedulableContext SC)
    {
        Cls_DepCalculationForCalPeriod ObjDCP = new Cls_DepCalculationForCalPeriod();
    }
}

 

Last but not least the class called within that -  Cls_DepCalculationForCalPeriod() is the very large class that contains the actual logic. It is seriously too big to post – and using conventional testing methods we are fairly certain that it does what it needs to do.

 

But I am totally IN LOVE with test classes and I think this methodology is AMAZING – so I am on a quest to make sure test classes ACTUALLY  test properly and thoroughly.

 

Question 1:

How is it possible that I am getting a 60% Code coverage on Cls_DepCalculationForCalPeriod?

I guess re-wording that question it seems to me that this test class does NOTHING - how can it be giving 60% ? 

 

Question 2:

Should my test class not be setting up a series of records in my ‘source’ objects and then somehow looking at the records generated in the destination objects and making sure they are what would be expected based on the test data I created in my test class?

 

Question 3 (a and b):

Along that train of thought how can I write a test class that IGNORES all existing data? What I mean is Cls_DepCalculationForCalPeriod will look at EVERY record in the source tables and generate data that will potentially include both any test data I set up as well as any data that happens to be sitting in the org at the time the test runs. How can I force a ‘pristine’ data set for the test?

 

Any assistance you can provide would be greatly appreciated.

 

AngiB 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Whenever you create an Apex Class (in Visual force terms it may be called a controller or an extension), and when you want to move your Apex Class to production it is necessary that you have a Test Method for your Apex Class. Salesforce wants you to ensure that the Apex Code that you deploy to your production instance will not cause any unexpected behaviour to you as well as to the them. That's the simple reason behind this.

 

Also if your class is giving 60% code coverage then it means that your Test method is executing only that much of your code. Moreover, the test coverage for your Apex Class should be NOT less than 75%.

 

Note: The data created in Test Method is only dummy data. So any DML operation such insert , update and Delete done in Test Method does not reflect changes to your org data.

 

Here also if you want to write a test class that IGNORES all existing data, for this I would like to inform you that  test methods don’t have access by default to pre-existing data in the organization (Starting with Apex code saved using Salesforce API version 24.0). IsTest (SeeAllData=true) Annotation is used for Apex code saved using SalesforceAPI version 24.0 and later, to grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test didn’t create.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.