function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sreenathsreenath 

webservice class testcoverage need help

 

Hi All,

 

It is in global class. testcoverage is 18% now. Here only first start method is under coverage. I have 7 methods in execute method.

 

 Webservice static void execute()
    {
    ScheduleJob_UpdateOpportunityInit.start();
    ScheduleJob_UpdateOpportunityInit.start2();
    ScheduleJob_UpdateOpportunityInit.start3();

}

 

@istest
private class Scheduledjob_test{
public static testMethod void Scheduledjobs()
{
Test.StartTest();
try{StartScheduledTasks.execute();     }
catch(Exception e7){System.debug('An Exception has Occured: '+e7);}
Test.stopTest();
}
}

 

 

need help...............Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The problem is that you already have the jobs scheduled, so when you attempt to schedule them in your test method, an exception occurs.  You may want to look at the Test.isRunningTest() method, so that you can skip the actual scheduling when running from a test method.

All Answers

bob_buzzardbob_buzzard

Are you getting errors when you attempt to test the other methods?

sreenathsreenath

thanks for your time

No, I don't have any errors in classes include in the webservice.

 

 

 

bob_buzzardbob_buzzard

Are these autogenerated classes from a wsdl?

sreenathsreenath

No, just written class, calling 3 class methods which contains simple schedulable statements.

 

public static void start()
     {
        system.debug('?');
       
         ScheduleJob_UpdateOpportunityStatusStart st = new ScheduleJob_UpdateOpportunityStatusStart();
        
         system.debug(st);
        
         String sch = '0 1 * * * ?'; //Every hour offset 1 min
         System.schedule('UpdateOpportunityStatus 1', sch, st);
     }

 

similar calls made from different classes.

bob_buzzardbob_buzzard

Which pieces of code are missing coverage then?  Is it the methods that schedule the code, or the actual scheduled code?

sreenathsreenath

Bob, Only the first line of start method under coverage in webservice class. this is the problem not under code coverage.

 the code is actual scheduled code, the methods using in webservice class in while calling these methods. no coverage for webservice class.

bob_buzzardbob_buzzard

I'm struggling a little to understand the problem here.

 

Is it that your scheduled code (which calls out to webservices) has no coverage?

sreenathsreenath

Very thankful for your time on this issue.

 

which i posted in start method only the scheduled code & different methods from different classes is calling

 

in webservice class execute method. all start methods are static, that's why calling from classname.

 

one person assigned task for test coverage to webservice classes. some are general we completed but this class having

 

different issue. you can see my code in start() only. similar methods using here.

 

 

bob_buzzardbob_buzzard

I'm afraid I'm still not clear where the coverage is missing - is it in the scheduled code, in the web service code or elsewhere?

sreenathsreenath

Bob, I need testcoverage for webservice class, its contains only calling methods of different classes as i shown in first post.

 

what the methods contain in different class is in Start (), which i shown. But under coverage only the first statement of any one class start() is covering & 18%  of coverage is displaying & remaining calls are not covering.

 

Thanks

Sreenath

bob_buzzardbob_buzzard

Can you post some more of the code?  I.e. the webservice class and an indication of the areas that don't have coverage.

sreenathsreenath

 

Hello Bob,

Thanks............

This is complete webservice class which i have to write testcoverage. All methods are static methods in this classes having few lines of code in it. I am not understanding the issue how can i do.

  global class StartScheduledTasks  { 

 

   Webservice static void execute()    { 

   ScheduleJob_UpdateOpportunityInitialize.start(); //under coverage

   ScheduleJob_UpdateOpportunityInitialize.start2(); // here onwards no coverage

   ScheduleJob_UpdateOpportunityInitialize.start3(); 

   InitiateTasksSchedular.Start(); 

   InitiateTasksSchedular.Start2(); 

   InitiateTasksSchedular.Start3(); 

   ScheduleJob_EmailTaskInitalize.Start(); 

   ScheduleJob_EmailTaskInitalize.Start2(); 

   ScheduleJob_EmailTaskInitalize.Start3(); 

   ScheduleJob_RentRollStart.Start(); 

   } 

 } 

 

 

bob_buzzardbob_buzzard

So you don't get any coverage, but you don't get any errors either?  Is there anything in the debug log that indicates why the test is stopping early?

sreenathsreenath

Hi Bob, I didn't find any issue in my debug log. please you can see once, you can find any issue.

 

 

03:16:02.789 (789805000)|METHOD_EXIT|[6]|01p500000000HTx|ScheduleJob_UpdateOpportunityInitialize.start()
03:16:02.789 (789853000)|METHOD_EXIT|[34]|01p500000000QiN|StartScheduledTasks.execute()
03:16:02.789 (789953000)|USER_DEBUG|[35]|DEBUG|An Exception has Occured: System.AsyncException: The Apex job named "UpdateOpportunityStatus 1" is already scheduled for execution.

bob_buzzardbob_buzzard

Here's the problem:

 

02:58:56.822 (822144000)|EXCEPTION_THROWN|[12]|System.AsyncException: The Apex job named "UpdateOpportunityStatus 1" is already scheduled for execution. 

 

Do you have existing jobs scheduled in the org?

sreenathsreenath

Thanks Bob,

            Yes 10 jobs are scheduled. But, I didn't understand here the issue.

 

3 classes : first class contain logic what to do when schedule.

                    second class contain method calls of first class & implements schedulable

                    third class contain start methods, it contains system.schedulable method.

 

I have three classes like third class as i explained.

 

I posted the webservice class contains all this three classes start() only.

How to write testcoverage for such class? 

 

Please explain when it as schedule in such case.

 

 

 

bob_buzzardbob_buzzard

The problem is that you already have the jobs scheduled, so when you attempt to schedule them in your test method, an exception occurs.  You may want to look at the Test.isRunningTest() method, so that you can skip the actual scheduling when running from a test method.

This was selected as the best answer
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Bob,

I am getting same error... for the test class can you please help me.
@istest
class TestEmailNotification {
    static testmethod void testEmailNotification() {
        System.schedule(  'Demo 1', '0 0 0 * * ?', new EmailNotificationScheduler() );   
        System.schedule(  'Demo 2', '0 0 0 * * ?', new EmailNotificationScheduler(1) );      
    }
}