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
thomsantthomsant 

Test Class implements Schedulable --- Urgent Help

I am trying to write a test class but i am  getting only 0% coverage , need help to get this fixed .Help appreciated.

 

 

 

global

class Migrate_LocAssign_Class_Batch implements Schedulable

 

{

 

 

globalvoid execute(SchedulableContext sc)

 

  {

 

     

MigrationDate__c MigDt =[SELECT Id,LastUpdateDate__c fromMigrationDate__cwhereNamelike'LocAssign%'orderby LastUpdateDate__c desc Limit 1];

      DateTime Dt = MigDt.LastUpdateDate__c;

      List<

Location_Assignment__c> location = [Select Id ,Account__c,Org_Unit__cfromLocation_Assignment__cwhere LastModifiedDate > :Dt];

     

      

//inset a new entry in the Custom Setting - MigrationDate

    

MigrationDate__c mg = newMigrationDate__c(Name='LocAssign'+ System.now(),LastUpdateDate__c=System.Today());

    

insert mg; 

     

 

//  Define connection id 

      Id networkId = ConnectionHelper.getConnectionId(

'Hartford Life GBD');

 

     

Set<Id> LASet = newSet<Id>();

     

Set<Id> AccSet = newSet<Id>();

      List<

Location_Assignment__c> Assignment = new List<Location_Assignment__c>();

     

// only share records created in this org, do not add accounts received from another org.

 

     

for (Location_Assignment__c newLocAssgn : location )

     {

      

if  (newLocAssgn.Id != null  )

         {

            LASet.add(newLocAssgn.Id);

            AccSet.add(newLocAssgn.account__c);

            Assignment.add(newLocAssgn);

          }    

        }

           

if (LASet.size() > 0)

            {

            List<

PartnerNetworkRecordConnection> LAConnections =  new  List<PartnerNetworkRecordConnection>();

            

for (Location_Assignment__c newLA : Assignment)

             {

               

if (LASet.contains(newLA.Id))

                {                

                    System.debug(

'AccountId'+newLA.account__c);

                   

PartnerNetworkRecordConnection newConnection =

                     

newPartnerNetworkRecordConnection(

                          ConnectionId = networkId,

                          LocalRecordId = newLA.Id,

                          SendClosedTasks =

false,

                          SendOpenTasks =

false,

                          SendEmails =

false,

                          ParentRecordId = newLA.account__c);

//newLA.account__c);

                          LAConnections.add(newConnection);

                } 

            }

           

if (LAConnections.size() > 0 )

             {

                   database.

insert(LAConnections);

             }

        }

  }   

}

colemabcolemab

You are probably getting 0% coverage because your code (as pasted) lacks a test method.

 

Here is a sample test method for a scheduleable apex class:

 

 static testmethod void Migrate_LocAssign_Class_Batch() {

		Test.startTest();
	   	
	   	// Schedule the test job  
	      String jobId = System.schedule('Migrate_LocAssign_Class_Batch',
	      				Migrate_LocAssign_Class_Batch.CRON_EXP, 
	         			new Migrate_LocAssign_Class_Batch());
	         			
	   // Get the information from the CronTrigger API object  
	      CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, 
	         NextFireTime
	         FROM CronTrigger WHERE id = :jobId];
	
	   // Verify the expressions are the same  
	      System.assertEquals(Migrate_LocAssign_Class_Batch.CRON_EXP, 
	         ct.CronExpression);
	
	   // Verify the job has not run  
	      System.assertEquals(0, ct.TimesTriggered);
	    	
	   Test.stopTest();
   	
   } // end testmethod Migrate_LocAssign_Class_Batch 

 

thomsantthomsant
Thanks for the reply , i will create the test class as defined in the reply. Tom