You need to sign in to do that
Don't have an account?
Code coverage is 100% is sandbox but when deploying to production it gives Code coverage error saying the code coverage is 71%
Can anyone help me with the Test class for the batch class(to delete records which are 180 days old) I have written, I'm getting 100% code coverage in sandbox but when I'm deploying the code in production and validating using "run specified test - giving the name of my test class" I'm getting Error as "Your Code coverage is 71%, you need at least 75% coverage to complete this deployment"
Here is my batch class and test class:
global class DeleteOpportunityProductSnapshotBatch implements Database.Batchable<sObject>, Schedulable
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator([SELECT Id FROM OpportunityLineItem_Snapshot__c WHERE CreatedDate != LAST_N_Days:180]);
}
global void execute(Database.BatchableContext BC, List<SObject> records)
{
delete records;
}
global void finish(Database.BatchableContext BC) { }
global void execute(SchedulableContext ctx)
{
Database.executeBatch(this);
}
}
Test Class:
@isTest
public class DeleteOppProductSnapshotBatch_Test
{
public static testMethod void testMethod1()
{
OpportunityLineItem_Snapshot__c Ols = new OpportunityLineItem_Snapshot__c();
Ols.Name = 'Test';
Ols.CreatedDate = Date.today().addDays(-180);
insert Ols;
Test.startTest();
DeleteOpportunityProductSnapshotBatch Dop = new DeleteOpportunityProductSnapshotBatch();
id batchprocessid = Database.executeBatch(Dop);
DeleteOpportunityProductSnapshotBatch sh1 = new DeleteOpportunityProductSnapshotBatch();
String sch = '0 0 2 * * ?';
system.schedule('Test Territory Check', sch, sh1);
Test.stopTest();
System.assert(batchprocessid != null);
}
Here is my batch class and test class:
global class DeleteOpportunityProductSnapshotBatch implements Database.Batchable<sObject>, Schedulable
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator([SELECT Id FROM OpportunityLineItem_Snapshot__c WHERE CreatedDate != LAST_N_Days:180]);
}
global void execute(Database.BatchableContext BC, List<SObject> records)
{
delete records;
}
global void finish(Database.BatchableContext BC) { }
global void execute(SchedulableContext ctx)
{
Database.executeBatch(this);
}
}
Test Class:
@isTest
public class DeleteOppProductSnapshotBatch_Test
{
public static testMethod void testMethod1()
{
OpportunityLineItem_Snapshot__c Ols = new OpportunityLineItem_Snapshot__c();
Ols.Name = 'Test';
Ols.CreatedDate = Date.today().addDays(-180);
insert Ols;
Test.startTest();
DeleteOpportunityProductSnapshotBatch Dop = new DeleteOpportunityProductSnapshotBatch();
id batchprocessid = Database.executeBatch(Dop);
DeleteOpportunityProductSnapshotBatch sh1 = new DeleteOpportunityProductSnapshotBatch();
String sch = '0 0 2 * * ?';
system.schedule('Test Territory Check', sch, sh1);
Test.stopTest();
System.assert(batchprocessid != null);
}
Based on your code, I see that you are not using SeeAllData=True which might reduce the code coverage as it queries data from specific org.
Are you running tests in the sandbox & deploying with different users/profiles? If so, missing some permission for the deployment user might reduce the coverage.
Related: https://help.salesforce.com/articleView?id=000335222&type=1&mode=1
Similar scenarios: https://salesforce.stackexchange.com/questions/76647/code-coverage-of-93-in-sandbox-but-only-61-in-production-wont-deploy
https://salesforce.stackexchange.com/questions/232434/code-coverage-error-when-deploying-to-another-sandbox-but-salesforce-doesnt-sh
https://developer.salesforce.com/forums/?id=906F00000009E5XIAU
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you