1

I have used schedule in class for my code. I am not able to cover code 100%. Its just cover 64% even i have write all condition.

Class

public void execute(SchedulableContext sc){

        Integer Year = Date.Today().Year();
        Boolean myData = false;
        List<Employment_Data__c> listEmp = new List<Employment_Data__c>();        

        List<Employment_Data__c> edata = [Select id, name from Employment_Data__c];
        for(Employment_Data__c empData : edata){
            if(empdata.name == String.valueOf(Year)){
                myData = true;                
                break;
            }                
        }

        List<Account> accData = [Select id, Name, Service_ID__c from Account where Service_ID__c != null];       
        System.debug('Total ::'+accData.size());                          

        if(myData != true){ // This condition for fulfilled in my test class
            for(Account act : accData){
                Employment_Data__c ed = new Employment_Data__c();
                ed.name = String.valueOf(Year);
                ed.County__c = act.Id;
                listEmp.add(ed);
            }
            insert listEmp;
        }    


    }        

Test class

static testMethod void testCreateEmpData(){

        Test.startTest();
        Database.QueryLocator QL;
        Database.BatchableContext BC;
        SchedulableContext sc;
        ScheduleCreateRecord SE = new ScheduleCreateRecord();        

        Account act = new Account(Name='testAct');                               
        insert act;

        Integer Year = Date.Today().Year();
        Year = Year + 1;

        Employment_Data__c ed = new Employment_Data__c(Name = String.valueOf(Year),County__c = act.Id);  
        insert ed;

        SE.execute(sc);         

        Test.stopTest();        
    }

In my Employment_Data__c object, name is all year like 2001, 2002 etc. I want to insert one record of next year using this schedule class. i.e. if i run this class now it will create one record for 2015. Give me suggestion how to cover that all code.

Thanks, vml

2
  • It would be helpful if you told us what lines in your code you aren't getting coverage on that you need help with. The first thing I notice about your code is that you're using Test.startTest() before you've even created your data necessary for it to run. You'll also want to assert results after the class runs; something that should happen at the end of your method, not at the beginning..
    – crmprogdev
    Commented Nov 27, 2014 at 16:22
  • Thanks for reply crmprogdev. In my class see this line . if(myData != true){ // This condition for fulfilled in my test class. This condition not cover code. And According you i have to write Test.startTest() after necessary data creation ?
    – Vml
    Commented Nov 28, 2014 at 5:16

1 Answer 1

1

Let me see if I can get you sorted here. When you have a branch or if-else condition like you have, you often need to create two test methods - one for the positive and another for the false condition.

That means you need to create a 2nd method where you don't create the test data that your query returns. When that happens, your If(myData != true) code section will execute. Once you do that, you'll have the coverage you need.

For your test class methods, you'll want to restructure them to something along the lines as follows:

static testMethod void testCreateEmpData1(){

    Account act = new Account(Name='testAct');                               
    insert act;

    Integer Year = Date.Today().Year();
    Year = Year + 1;

    Employment_Data__c ed = new Employment_Data__c(Name = String.valueOf(Year),County__c = act.Id);  
    insert ed;

    Database.QueryLocator QL;
    Database.BatchableContext BC;
    SchedulableContext sc;
    ScheduleCreateRecord SE = new ScheduleCreateRecord();        

    Test.startTest();

    SE.execute(sc);         

    Test.stopTest();        

    // Add system asserts here for what you expect to happen after the batch executes.

}

Then to test the If statement

static testMethod void testCreateEmpData2(){

    Account act = new Account(Name='testAct');                               
    insert act; // you only need to create the account. Your class does the rest.

    Database.QueryLocator QL;
    Database.BatchableContext BC;
    SchedulableContext sc;
    ScheduleCreateRecord SE = new ScheduleCreateRecord();        

    Test.startTest();

    SE.execute(sc);         

    Test.stopTest();        

    // Add system asserts here for what you expect to happen after the batch executes.

}
1
  • 1
    to add to crmprgdev's response -- separate testmethods are required because in a single testmethod, only one schedulable event can be tested within a Test.starttest() and Test.stopTest() bracket and you can only have one Test.startTest() per testmethod
    – cropredy
    Commented Nov 28, 2014 at 18:54

You must log in to answer this question.

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