0

folks, I have a scheduled class from which I am calling my future method but I am not able to increase test coverage for certain section please help me with it.

Scheduled class (100 % covered)

global class ActivateCFSUsers Implements Schedulable {

    public Interface IScheduleDispached {
        void execute(SchedulableContext sc);
    }
    global void execute(SchedulableContext SC) {

        Type targetType = Type.forName('HANDLERNAME');
        if(targetType != null){
            IScheduleDispached obj =(IScheduleDispached)targetType.newInstance();
            obj.execute(sc);
        }
    }
}

HANDLER CLASS That is being called (75% covered)

public class HANDLERNAME implements ActivateCFSUsers.IScheduleDispached {

     public void execute(SchedulableContext sc) {

        List<User> newusers = [Select Id,CFS_Activation_Date__c from user where (Profile.Name='CTCT Solution Provider (Login Based)'OR Profile.Name='CTCT Solution Provider (Named)')AND lmscons__Cornerstone_ID__c=NULL AND lmscons__CFS_Status__c='Not CFS user']; 

        Set<Id> sendset = new Set<Id>();
        for(integer i=0;i<newusers.size();++i)
            // Next line is not covered
            sendset.add(newusers[i].Id);  
        if(newusers.size()>0)
            System.debug(newusers.size()+' new users found will give them license in a sometime');
        if(newusers.size()>0)
            //Next line which is call to my future method is not covered
            UserHandler.enrollCFSUser(sendset);
    } 

}

Test Class (I am not testing anything as of now my first objective is to increase coverage)

@isTest
private class testsActivateCFSUsers {
@isTest
private static void test() {
Test.StartTest();
ActivateCFSUsers sh1 = new ActivateCFSUsers();
String sch = '0 0 23 * * ?'; 
system.schedule('Test Territory Check', sch, sh1); 
Test.stopTest(); 
}   
}
4
  • post your test class Commented May 5, 2017 at 5:20
  • @RohitMourya Updated
    – Ishan
    Commented May 5, 2017 at 5:24
  • @RohitMourya I cannot see your answer Code coverage increased to 100% thanks man
    – Ishan
    Commented May 5, 2017 at 5:47
  • Now you can see it Commented May 5, 2017 at 5:47

1 Answer 1

0

You're not creating test data properly. You're querying User but haven't created any user in your test class. Because of this, your query results 0 records. It's a standard practice to create test data for your controller. Refer below code in which I've created a user with profile which you're referring in your query. Though below code is not tested but it will give some idea:

@isTest
private class testsActivateCFSUsers {
    @isTest
    private static void test() {
        Profile p = [select id,Name from profile where name = 'CTCT Solution Provider (Login Based)'];

        User TestUser = new User(alias = 'standt', email = '[email protected]', MobilePhone = '9827261629',
                                 emailencodingkey = 'UTF-8', lastname = 'Testing', languagelocalekey = 'en_US',
                                 localesidkey = 'en_US', profileid = p.Id, Mobile_number__c = '9827261629',
                                 timezonesidkey = 'America/Los_Angeles', username = '[email protected]');
        insert TestUser;

        Test.StartTest();
        ActivateCFSUsers sh1 = new ActivateCFSUsers();
        String sch = '0 0 23 * * ?'; 
        system.schedule('Test Territory Check', sch, sh1); 
        Test.stopTest(); 
    }   
}
4
  • You don't have to create users in test classes. Users data is pre-available in test classes. For details check this article.
    – Mahmood
    Commented May 5, 2017 at 5:38
  • @Mahmood if that's the case then code coverage should have been 100% Commented May 5, 2017 at 5:48
  • @Mahmood I think this in not the best practice to use actual data in the test class.
    – Ishan
    Commented May 5, 2017 at 6:16
  • 1
    Users are visible in test methods. Maybe none of them matched the criteria though.
    – Eric
    Commented May 5, 2017 at 7:27

You must log in to answer this question.

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