0

This question already has an answer here:

Below I have shared the APEX code and the Test class code. I receive 100% code coverage, but I cannot grasp certain parts of it. Eg. The system.assert function - is it necessary? How do I know my test class is actually testing a code. I have only created sample records eg. acc1, con1 etc., how do I know if the test class is actually tested. Will I get messages if certain aspects of the test class is run or executed successfully? What should I add/remove here? I am new to APEX, please give me your onion.

Apex code:

global class setExams2Expired implements Schedulable {
    global void execute(SchedulableContext ctx) {

        List<Certification__c> objects = [
        SELECT Id, Expiration_Date__c, Exam_state__c
        FROM Certification__c
        WHERE Exam_state__c in ('Active')];

        for(Certification__c c : objects){
            if(c.Expiration_Date__c < date.today()){
                c.Exam_state__c = 'Expired'; 
            }
            else if(c.Expiration_Date__c == date.today().addDays(+7)){
                c.Day7ExpiryAlert__c = True;
            }
        }
        update objects;
    }
}

Test Class:

@isTest
public class setExams2ExpiredTest { 
    @isTest
    public static void ifNotExpired() {

        Account acc1 = new Account(name='Test Account 1',phone='121212', Global_POD__c ='AP');
        insert acc1;
        Account acc2 = new Account(name='Test Account 2',phone='343434', Global_POD__c ='AP');
        insert acc2;

        Contact con1 = new Contact(accountid=acc1.id,lastname='Test Contact 1',email='abcd@gmail.com');
        insert con1;
        Contact con2 = new Contact(accountid=acc2.id,lastname='Test Contact 2',email='efgh@gmail.com');
        insert con2;

        Certification__c c = new Certification__c(Contact__c=con1.id, Name = 'abcd', Expiration_Date__c = Date.today().addDays(+2), Exam_state__c = 'Active');
        insert c;

        Test.startTest();
        setExams2Expired ctx = new setExams2Expired();
        ctx.execute(null);
        System.assertEquals('Active','Active','ifNotExpired doesn\'t work - meaning an exam is either set to expired when it shouldn\'t have been or nothing happenend for a certain record');
        Test.stopTest();
    }
    @isTest
    public static void sendEmail(){

        Account acc5 = new Account(name='Test Account 5',phone='787878', Global_POD__c ='AP');
        insert acc5;
        Account acc6 = new Account(name='Test Account 6',phone='898989', Global_POD__c ='AP');
        insert acc6;

        Contact con5 = new Contact(accountid=acc5.id,lastname='Test Contact 5',email='qrstuv@gmail.com');
        insert con5;
        Contact con6 = new Contact(accountid=acc6.id,lastname='Test Contact 6',email='wxyz@gmail.com');
        insert con6;

        Certification__c c = new Certification__c(Contact__c=con5.id, Name = 'efgh', Expiration_Date__c = Date.today().addDays(+7), Exam_state__c = 'Active', Day7ExpiryAlert__c = false);
        insert c;

        Test.startTest();
        setExams2Expired ctx = new setExams2Expired();
        ctx.execute(null);
        System.assertEquals(false, false,'No alert was sent here');
        Test.stopTest();
    } 
}

UPDATE 15th/03/2019

Test.startTest();
setExams2Expired ctx = new setExams2Expired();
ctx.execute(null);
Map<Id, Certification__c> cMap = new Map<Id, Certification__c>([Select id, Exam_state__c, Day7ExpiryAlert__c FROM Certification__c]);
System.assertEquals('Active', String.valueOf(cMap.get(c.Exam_state__c)));
Test.stopTest();

Working Update

Test.startTest();
setExams2Expired ctx = new setExams2Expired();
ctx.execute(null);
Map<Id, Certification__c> cMap = new Map<Id, Certification__c>([Select Exam_state__c FROM Certification__c]);
System.assertEquals('Certification__c:{Exam_state__c=Expired, Id='+c.id+'}', String.valueOf(cMap.get(c.Id)), 'setting to expired failed');
Test.stopTest();

marked as duplicate by Pranay Jaiswal, Reshma, glls, Lukas Lunow, battery.cord Mar 15 at 13:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

You should place your execute between Test.StartTest() and Test.StopTest() instructions.

After that, you should recover your data using a SOQL query to retrieve and assert it.

Test.startTest();
setExams2Expired ctx = new setExams2Expired();
ctx.execute(null);

Map<Id, Certification__c> certificationMap = new Map<Id, Certification__c>([Select id, Exam_state__c, Day7ExpiryAlert__c FROM Certification__c]);

System.assertEquals(<ExpectedValue>, certificationMap.get(c.id));

Test.stopTest();

If an exception is being thrown, you could handle it with a try-catch block and assert it.

Boolean exceptionThrown = false;

Test.startTest();
setExams2Expired ctx = new setExams2Expired();

try {
   ctx.execute(null); 
} catch(Exception e) {
   exceptionThrown = true;
}

System.assertEquals(true, exceptionThrown);
Test.stopTest();
  • Is it necessary to create more than one entry for testing, for example, i have created 2 contacts, 2 accounts etc. – Niveth Kumar Mar 14 at 10:07
  • 1
    It depends on your logic, on how much data to execute do you need. You should read about white box and black box testing to understand the fundamentals of testing in Salesforce. – Dahn Mar 14 at 14:38
  • Will do, on the other hand, can you look at the updated section of my question. I did the changes like you suggested, but it fails, and gives me following message : System.StringException: Invalid id: Active. – Niveth Kumar Mar 15 at 6:26
  • 1
    I figured it out on my own, with your guidance, thank you – Niveth Kumar Mar 18 at 8:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.