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
Deepak agarwal 9Deepak agarwal 9 

Batch a not getting executed from Test Class

Hi Guys,

 I Have a very different situation. I have a batch apex and test class.Now my requirement is executing batch from test class.When i am running test class the debug statements of Start,Execute and Finish Methods are getting displayed but in Apex Jobs there is no sign of the batch.Like the Batch name and all are not getting displayed in Apex Jobs.
global class BatchClassexample implements Database.Batchable<sObject> {
    global String query = 'SELECT Id,Name FROM Account where name=:filter ';
    global Database.QueryLocator start(Database.BatchableContext BC)
        system.debug('inside methiod----');
        String filter='Client';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<Account> scope)
        //User u=[SELECT username from user where profileid='00e28000000sxOR'];
        system.debug('inside execute::'+scope);
        for(Account a : scope)
            a.Name = a.Name + '(newyork)';
            System.debug('::::::: execute');
            update a;
        system.debug('scope ::: '+scope);	
       // update scope;
        system.debug('scope after ::: '+scope);
    global void finish(Database.BatchableContext BC) {
        system.debug('inside finish');

//Test class
  public class BatchClassexampleTestClass {
        public static testMethod void testRunAs(){       
        BatchClassexample BC=new BatchClassexample();
        Account a=new Account(name='Client');
            insert a;
		/*Profile p = [SELECT Id FROM Profile WHERE Name='Standard Platform User']; 
      User u = new User(Alias = 'standt', Email='', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='');
            insert u;*/
            User u=[SELECT username,ProfileId from user where profileid='00e28000000sxOR'];
               System.debug('u :::::: '+u);
				System.debug('u.ProfileId :::::: '+u.ProfileId);   
               System.debug('name ::: '+a.Name);
               System.debug('name after ::: '+a.Name);

Naval Sharma4Naval Sharma4
Hi Deepak,

A batch which gets executed in test classes will not appear under Apex Jobs.
Deepak agarwal 9Deepak agarwal 9
Hi Naval,

Thanks for your reply.But i have a requirement--When a user submits any batch class the submittedby name should be other user.So i thought of writting test class which uses System.RunAs() method. So is my approach correct??
Naval Sharma4Naval Sharma4
Your approach is correct but if you want to assert it then you will have to query on the CronTrigger object. For more info Batch Apex. (
Deepak agarwal 9Deepak agarwal 9
But CronTrigger Object is used to query scheduled jobs in the organisation.I need to change the submittedBy user name