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(); 
}   
}
  • post your test class – Rohit Mourya May 5 '17 at 5:20
  • @RohitMourya Updated – Ishan May 5 '17 at 5:24
  • @RohitMourya I cannot see your answer Code coverage increased to 100% thanks man – Ishan May 5 '17 at 5:47
  • Now you can see it – Rohit Mourya May 5 '17 at 5:47
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 = 'standardusersdf23423@testorg.com', 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 = 'test1User123sdf234234@test.com');
        insert TestUser;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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