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
hmoh1920hmoh1920 

batch deployement

Hi,

i write a batch and  test method into this batch, my code of this batch is 87% couvred into my sand box  but  when I try to deploy in production I get an error only 12% of the batch code is covered.

 

this code of my test metode:

/***********************************************************************/

public static testmethod void testbatch()
{
    
  Test.startTest();
  BalanceCalculBatch ch = new BalanceCalculBatch();
  ID batchprocessid = Database.executeBatch(ch);
  Test.stopTest();
           
}

 

/*********************************************************************/

 

 

this is my match code:

 

/*********************************************************************/

global class BalanceCalculBatch implements Database.Batchable<sObject>{
String IDVar;
decimal var1=0, var2=0;
date dateEcheance;


String email;
Id toUserId;
Id fromUserId;

        
global final String query ='SELECT id,name FROM Opportunity';        

global Database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}

global void execute(Database.BatchableContext BC, List<Sobject> scope){

  List<Opportunity> opps = [select id,  Date_d_ech_ance_de_la_police__c, Total_montant_pay__c
                            from Opportunity  where Date_d_ech_ance_de_la_police__c > 2011-12-31];
   
   
    Integer l;
    l = opps.size();
if(l!=Null) {                       
    for (Integer i = 0; i<l; i++)
              {
                  IDVar=opps[i].Id;

 

//my traitement code

 

}

}

}

 

 

 

 

 

Help me, where is the problems?

 

thanks.

 

 

 

Moh.

 

Damien_Damien_

Yes, i've had similar problems with my batch classes.  Have your batch class pass the scope into a helper method and do the work there.  That way you can pass a scope you specify into the helper method and it be guaranteed to execute.

hmoh1920hmoh1920

Hi,

 

how? I'm a beginner!

 

 

thanks

Damien_Damien_

I'm assuming that Salesforce is the first time you've ever written code.

 

Anything you pass into a method, you can use inside of it like its a variable you have already initialzed to a value.  I made some other minor adjustments because you did some other unneccessary checks.  An empty list will never return null, it will simply return a value of 0.

 

The value you are passing into the Database.getQueryLocator is what turns into your scope in the execute method.  The only issue with this is that it ONLY takes SObject.  So your Opportunity inherits from SObject which means that it is technically an SObject, but just a specific type of instance of it.  Since it is of type SObject, we need it to be of type Opportunity when we do work on it.  Because of this, I cast it to a List of Opportunities since thats what the query we called earlier specified it as, but it changed to its generic version when passed into the method since thats what it requires.

 

I'm not sure why Salesforce doesn't correctly test there batches.... or why they haven't fixed it yet after all this time, but this should get you as much coverage as you're going to get from a batch.

 

Hopefully this all made sense to you.... good luck.

 

Batch Code:

global class BalanceCalculBatch implements Database.Batchable<sObject>
{
	global final String query ='SELECT id,name FROM Opportunity';        

	global Database.querylocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator([select id,  Date_d_ech_ance_de_la_police__c, Total_montant_pay__c
			from Opportunity  where Date_d_ech_ance_de_la_police__c > 2011-12-31]);
	}

	global void execute(Database.BatchableContext BC, List<Sobject> scope)
	{
		BalanceCalculBatch.doExecution((List<Opportunity>) scope);
	}
	
	public static void doExecution(List<Opportunity> scope)
	{
		for (Integer i = 0; i < scope.size(); i++)
		{
			String IDVar = [i].Id;
	 
			//my traitement code
		}
	}
}

 Test Code:

public static testmethod void testbatch()
{
  Test.startTest();
  BalanceCalculBatch ch = new BalanceCalculBatch();
  ID batchprocessid = Database.executeBatch(ch);
  BalanceCalculBatch.doExecution([SELECT id,  Date_d_ech_ance_de_la_police__c, Total_montant_pay__c FROM Opportunity WHERE Date_d_ech_ance_de_la_police__c > 2011-12-31]);
  Test.stopTest();
}