+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

test class Please help me

-----My class cover 64 % how to increase covarge----
global class OnDemandSandboxSchedulerV3 implements Schedulable,Database.AllowsCallouts{

    global void execute(SchedulableContext sc){
        expiredDate();
        dailyCtrlOpr();
    }

    global void expiredDate(){
        List<ManageOnDemandS__c> lstmanagedemand = [Select Id,Sandbox_Id__c,Name,Expire_Date__c,OnDemandRealms__c from ManageOnDemandS__c where Expire_Date__c = today AND Status__c != 'Deleted Sandbox'];
        System.debug('----lstmanagedemand--'+lstmanagedemand);
        if(!lstmanagedemand.isEmpty()){
            //Using JSON.Serialize method we can convert the account list to Json
            String sandboxData = JSON.Serialize(lstmanagedemand);
            System.debug('sandboxData---'+sandboxData);
            //Calling the handler to delete the Sandbox
            OnDemandSandboxIntegrationClass.deleteSandbox(sandboxData);
        }  
    }

    global void dailyCtrlOpr(){
        DateTime now = DateTime.now();
        System.debug('---now'+now);
        String dayOfWeek;
        String hour;
        if(test.isRunningTest()){
            dayOfWeek = 'Saturday';
        }else{
          dayOfWeek = now.format('EEEEE');
        }
         //String dayOfWeek = 'Saturday';
        System.debug('---dayOfWeek'+dayOfWeek);
        if(test.isRunningTest()){
            hour = '2';
        }else{
          hour = String.valueOf(now.hour());
        }
        System.debug('---hour'+hour);
        
        List<ManageOnDemandS__c> lstToStop = new List<ManageOnDemandS__c>();
        List<ManageOnDemandS__c> lstToStart = new List<ManageOnDemandS__c>();
        for(ManageOnDemandS__c mngSndBox:[Select Id, Action__c, Sandbox_Id__c, Status__c, DailyStart__c, DailyStop__c, Friday_Start__c, Friday_Stop__c, Monday_Start__c, Monday_Stop__c, Saturday_Start__c, Saturday_Stop__c, Sunday_Start__c, Sunday_Stop__c, Thrusday_Start__c, Thrusday_Stop__c, Tuesday_Start__c, Tuesday_Stop__c, Wednesday_Start__c, Wednesday_Stop__c, Weekend_Stop_Re_Start__c, Monday_Checkbox__c, Tuesday_Checkbox__c, Wednesday_Checkbox__c, Thrusday_Checkbox__c, Friday_checkbox__c, Saturday_Checkbox__c, Sunday_Checkbox__c, OnDemandRealms__c FROM ManageOnDemandS__c WHERE (Action__c IN ('start','stop','Create Sandbox')) LIMIT 50000]){
        System.debug('---mngSndBox'+mngSndBox);
		
            if(mngSndBox.Action__c == 'start' || mngSndBox.Action__c == 'Create Sandbox'){
                if(dayOfWeek == 'Monday' && mngSndBox.Monday_Checkbox__c == true && mngSndBox.Monday_Start__c == hour){
                    lstToStop.add(mngSndBox);
                }else if(dayOfWeek == 'Tuesday' && mngSndBox.Tuesday_Checkbox__c == true && mngSndBox.Tuesday_Start__c == hour){
                    lstToStop.add(mngSndBox);
                }else if(dayOfWeek == 'Wednesday' && mngSndBox.Wednesday_Checkbox__c == true && mngSndBox.Wednesday_Start__c == hour){
                    lstToStop.add(mngSndBox);
                }else if(dayOfWeek == 'Thursday' && mngSndBox.Thrusday_Checkbox__c == true && mngSndBox.Thrusday_Start__c == hour){
                    lstToStop.add(mngSndBox);  
                }else if(dayOfWeek == 'Friday' && mngSndBox.Friday_checkbox__c == true && mngSndBox.Friday_Start__c == hour){
                    lstToStop.add(mngSndBox);
                }else if(dayOfWeek == 'Saturday' && mngSndBox.Saturday_Checkbox__c == true && mngSndBox.Saturday_Start__c == hour){
                    lstToStop.add(mngSndBox);
                }else if(dayOfWeek == 'Sunday' && mngSndBox.Sunday_Checkbox__c == true && mngSndBox.Sunday_Start__c == hour){
                    lstToStop.add(mngSndBox);
                }
            }else if(mngSndBox.Action__c == 'stop'){
                if(dayOfWeek == 'Monday' && mngSndBox.Monday_Checkbox__c == true && mngSndBox.Monday_Stop__c == hour){
                    lstToStart.add(mngSndBox);
                }else if(dayOfWeek == 'Tuesday' && mngSndBox.Tuesday_Checkbox__c == true && mngSndBox.Tuesday_Stop__c == hour){
                    lstToStart.add(mngSndBox);
                }else if(dayOfWeek == 'Wednesday' && mngSndBox.Wednesday_Checkbox__c == true && mngSndBox.Wednesday_Stop__c == hour){
                    lstToStart.add(mngSndBox);
                }else if(dayOfWeek == 'Thursday' && mngSndBox.Thrusday_Checkbox__c == true && mngSndBox.Thrusday_Stop__c == hour){
                    lstToStart.add(mngSndBox);
                }else if(dayOfWeek == 'Friday' && mngSndBox.Friday_checkbox__c == true && mngSndBox.Friday_Stop__c == hour){
                    lstToStart.add(mngSndBox);
                }else if(dayOfWeek == 'Saturday' && mngSndBox.Saturday_Checkbox__c == true && mngSndBox.Saturday_Stop__c == hour){
                    lstToStart.add(mngSndBox);
                }else if(dayOfWeek == 'Sunday' && mngSndBox.Sunday_Checkbox__c == true && mngSndBox.Sunday_Stop__c == hour){
                    lstToStart.add(mngSndBox);
                }
            }
        }
        System.debug('---lstToStop'+lstToStop);
        System.debug('---lstToStart'+lstToStart);
        //System.debug('---lstMngSndBox'+lstMngSndBox);
        if(!lstToStart.isEmpty()){
            String sndBoxStart = JSON.Serialize(lstToStart);
            OnDemandSandboxIntegrationClass.controlSandbox(sndBoxStart,'daily-start');
        }if(!lstToStop.isEmpty()){
            String sndBoxStop = JSON.Serialize(lstToStop);
            OnDemandSandboxIntegrationClass.controlSandbox(sndBoxStop,'daily-stop');
        }
    }
}

///------My Test Class-----///
@isTest
public class OnDemandSandboxSchedulerV2Test {
    static testmethod void Testscheduledmethod(){
        String CRON_EXP = '0 0 2 * * ? *';
        DateTime now = DateTime.now();
        String  status = 'weekend-stop';
        OnDemandRealms__c objdem = New OnDemandRealms__c();
        objdem.RealmID__c = 'bdtv';
        objdem.Key__c = '6776c02b-6981-4234-a73a-6528a04cd30e';
        objdem.keypassword__c ='87900';
        insert objdem;
        
        ManageOnDemandS__c managdemand = New ManageOnDemandS__c();
        managdemand.Expire_Date__c = System.today();
        managdemand.Status__c = 'Sandbox';
        managdemand.Action__c = 'Create Sandbox';
        managdemand.Weekend_Stop_Re_Start__c = true;
        managdemand.DailyStop__c = '2';
        managdemand.DailyStart__c = '2';
        managdemand.OnDemandRealms__c = objdem.Id;
        insert managdemand;
      
        Test.setMock(HttpCalloutMock.class, new RestMock());
        test.startTest();  
        OnDemandSandboxSchedulerV3 obj = New OnDemandSandboxSchedulerV3();
        String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, obj);
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
        System.assertEquals(CRON_EXP, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);
        test.stopTest();
    }
    
    static testmethod void Testscheduledmethod1(){
        String CRON_EXP = '0 0 2 * * ? *';
        DateTime now = DateTime.now();
        String  status = 'weekend-stop';
        OnDemandRealms__c objdem = New OnDemandRealms__c();
        objdem.RealmID__c = 'bdtv';
        objdem.Key__c = '6776c02b-6981-4234-a73a-6528a04cd30e';
        objdem.keypassword__c ='87900';
        insert objdem;
        
        ManageOnDemandS__c managdemand = New ManageOnDemandS__c();
        managdemand.Expire_Date__c = System.today();
        managdemand.Status__c = 'Sandbox';
        managdemand.Action__c = 'Create Sandbox';
        managdemand.Weekend_Stop_Re_Start__c = true;
        managdemand.DailyStop__c = '2';
        managdemand.DailyStart__c = '2';
        managdemand.OnDemandRealms__c = objdem.Id;
        insert managdemand;
        
        managdemand.Action__c = 'Stop';
        update managdemand;
      
        Test.setMock(HttpCalloutMock.class, new RestMock());
        test.startTest();  
        OnDemandSandboxSchedulerV3 obj = New OnDemandSandboxSchedulerV3();
        String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, obj);
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
        System.assertEquals(CRON_EXP, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);
        test.stopTest();
    }
    
    public class RestMock implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest req) {
            String json = '{"operation":"test"}';
            HTTPResponse res = new HTTPResponse();
            res.setHeader('accept','application/json');
            res.setBody(json);
            res.setStatusCode(200);
            return res;
        }
    }
}

 
AbhishekAbhishek (Salesforce Developers) 
Code coverage is a measurement of how many unique lines of your code are executed while the automated tests are running. Code coverage percentage is the number of covered lines divided by the sum of the number of covered lines and uncovered lines.

For purposes of calculating code coverage, only executable lines of code are counted, whether covered or uncovered. Comments and blank lines are not counted, and System.debug() statements and curly braces that appear alone on one line are also not counted.

While writing unit tests, the main emphasis should be given on actually testing the logic and behavior of the block by designing input data to ensure the code path executes and writing assertions to make sure the code's behavior is as expected. Code coverage is a side effect of this testing process.
Fundamentally, to increase your code coverage, you must write functional unit tests for code paths that are not currently covered. For more community resources on writing high-quality unit tests

Go through the below blog which will guide you how to write the test class easily,

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.