0

I have written an scheduler class inside which I have methods too, so that I can schedule the same class at different schedule time and the methods works based on current time, MY CODE

global class testclass Implements Schedulable
{
    global void execute(SchedulableContext sc)
        {
            String t= String.valueOf(System.now());
            integer t1 = integer.valueOf(t.substring(11,13));
            if(t1<13)
            {
                morningfn();
            }
            else 
            {
                eveningfn();    
            }
        }
   morningfn()
   {
   //CODE
   }
   eveningfn()
   {
   //CODE
   }
 }

I have Scheduled the small class twice a day i.e.morning 8 and evening 5 so it will class the function based on the current time and this is working fine. but while writing test class

@isTest
public class testclasstest {
@isTest Static void test1(){
testclass  tc = new testclass ();
String sch = '0 30 10 * * ?';
System.schedule('morning', sch, tc);
System.schedule('evening', sch, tc);
    }

}

When I execute this test it ll be always 53% based on what time executing either before 13 i.e.1PM (Morning function) or after 13 (evening function).

I can achieve 100% if i remove if-then or make 2 different class or scheduler class but i want to achieve everything in single class.

is there any way I can call function name in system.schedule method???? or any other solution Plz Help. Thank You in advance.

0
Please try this. The solution is to override the t variable from test class

    Main class
    //global variable 
    public Integer timeFromTest;
    public Boolean flag = false;

    String t= String.valueOf(System.now());
    integer t1 = integer.valueOf(t.substring(11,13));
    if(flag) {
        t = timefromTest ;
    }


test class:
//1st time
scheduleClass.flag = true;
Scheduleclass.timefromtest = 14;
//2nd time
Scheduleclass.timefromtest = 10;
  • Thank you @Shravan, this is quite easy but I got the following error Method does not exist or incorrect signature: Test.isRunningTest() not only this whenever I use TEST like test.starttest or test.stoptest I get this error can u explain why?? – Pramodh S Jun 5 '17 at 11:46
  • Modified answer. 'Test' class scope is with in the test class(means it should be @isTest). – Shravan Boddula Jun 5 '17 at 12:15
  • Thanks @Shravan You saved me lot of time its working, and I had some dummy class with name 'test' so it was throwing error I deleted that class now test.isRunningtest() is working. – Pramodh S Jun 5 '17 at 12:32
0

Assign the "@testVisible" attribute to your private methods, so you can call them directly from the unit tests, see: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm

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.