I have a question, I'm making a test class for a batch. This batch deletes custom objects depending on the date (for example, the constructor receives an amount of days, and the batch deletes all those objects older than those days). Here is the class:

global with sharing class BatchApexDeleteQuestions implements Database.Batchable<sObject>, Database.Stateful{

private Integer cantidadDias;
private Integer cantidadMeses;
private Map<String,Integer> mapMeses;

global BatchApexDeleteQuestions(Integer cantidadDias){
    this.cantidadDias = cantidadDias;
    this.cantidadMeses = null;

global BatchApexDeleteQuestions(Map<String,Integer> mapMeses){
    if(mapMeses != null && mapMeses.containsKey('QUESTIONS')){
        this.cantidadMeses = mapMeses.get('QUESTIONS');
        this.cantidadMeses = 0;
    this.mapMeses = mapMeses;
    this.cantidadDias = null;

global Database.QueryLocator start(Database.BatchableContext BC){
    ERPvs.ApexLogger.Debug('>>> BATCH START');
    Datetime dateLimit;
    if(cantidadDias != null){
        dateLimit = Datetime.now();
        dateLimit = dateLimit.addDays((cantidadDias * -1));
    }else if(cantidadMeses != null){
        Date dateAux = Date.today();
        dateAux = dateAux.addMonths(cantidadMeses * -1).toStartOfMonth();
        dateLimit = Datetime.newInstance(dateAux.year(),dateAux.month(),dateAux.day());
    return Database.getQueryLocator([SELECT Id FROM Question__c WHERE Question_Date__c <= :dateLimit]);

global void execute(Database.BatchableContext BC, List<Question__c> scope){
    ERPvs.ApexLogger.Debug('>>> BATCH Execute');
    delete scope;

global void finish(Database.BatchableContext BC){
    ERPvs.ApexLogger.Debug('>>> BATCH Finish');
    if(mapMeses != null){
        Database.executeBatch(new BatchApexDeleteItems(mapMeses), 200);


here is my simple test class:

public class Test_BatchApexDeleteQuestions {

static void setup(){
    TestFactory instance = TestFactory.instance;

    Question__c question1 = instance.create(new Question__c(
        Question_Date__c = DateTime.now().addDays(-40)

    Question__c question2 = instance.create(new Question__c(
        Question_Date__c = DateTime.now().addDays(-50)

    Question__c question3 = instance.create(new Question__c(
        Question_Date__c = DateTime.now().addDays(-60)


static testmethod void main(){

    BatchApexDeleteQuestions batch = new BatchApexDeleteQuestions(39);
    Id batchId = Database.executeBatch(batch);



My question is, if I run my test, will it delete only the questions I'm defining in my setup, or will it affect all the other questions that match the criteria?

1 Answer 1


Your Apex unit tests have no access to data outside the test context unless @seeAllData=true is specified (which you should almost never do). Even if you have visibility to the data through that annotation, changes made to data during test context is always rolled back at the end of the test.

Running a test should never impact your real org data. If you were to find a situation where it did, that would be a serious bug that needs to be reported and fixed.

  • thanks a lot! I know it seems like a pretty basic question I did, but I didn't want to make a guess and then lose data I didn't want to lose. Apr 21, 2021 at 15:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .