0

this code not cover test for 100%

global class closeAllDealsMonthAgo implements 
   Database.Batchable<SObject>, Database.Stateful{ 

   List<Opportunity> listRecords = new List<Opportunity>();
   global Database.QueryLocator start(Database.BatchableContext BC){
         String query = 'Select Id, Name, CloseDate,createdDate,StageName From Opportunity WHERE CloseDate=THIS_MONTH';
         return Database.getQueryLocator(query);
   }
   global void execute(Database.BatchableContext BC, List<SObject> scope){
      for(Opportunity obj : (Opportunity[]) scope){  
         if(Date.Today()>obj.CloseDate && (obj.StageName != 'Closed Won' || obj.StageName !='Closed Lost')){
            obj.StageName = 'Closed Lost';
            listRecords.add(obj);
         }
         if(Date.Today()>obj.createdDate.addMonths(1) && (obj.StageName != 'Closed Won' || obj.StageName !='Closed Lost')){
            obj.StageName = 'Closed Lost';
            obj.CloseDate= Date.today();
            listRecords.add(obj);
         }
      }
   }
   global void finish(Database.BatchableContext BC){
      if(!listRecords.isEmpty()){
         update listRecords;
      }
   }      
}

test code

@isTest             
private class closeAllDealsMonthAgoTest {
    static testmethod void testbatch() {
        Integer count = 200;
        Account acc = new Account(Name='Test Account');
        insert acc;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (integer i=0; i<count; i++){
            Opportunity opp = new Opportunity();
            opp.AccountId=acc.Id;
            opp.Name='My Opportunity '+i;
            opp.StageName='Qualification';
            opp.CloseDate= Date.today().addDays(-5);
            opps.add(opp);
        }
        insert opps;
        System.assertEquals(count, opps.size());
       
       Test.startTest();
           closeAllDealsMonthAgo obj = new  closeAllDealsMonthAgo();
           Database.executeBatch(obj);
       Test.stopTest();
      
         List<Opportunity> oppUpdatedList = [SELECT Id,StageName,CloseDate FROM Opportunity];
        for(Opportunity o : oppUpdatedList){
            if(Date.Today()>o.CloseDate && (o.StageName != 'Closed Won' || o.StageName !='Closed Lost')){
                o.StageName = 'Closed Lost';
                System.assertEquals('Closed Lost', o.StageName);    
            }
        }
    }
}
1
  • You need to create test data that guarantees that you enter the relevant loops and/or if statements. Relying on CreatedDate presents some difficulties for unit testing. You should look into using test.loadData(), which will allow you to specify the createdDate. – Derek F Jan 3 at 20:10
0

I was able to achieve 100% code coverage using the below code.

@isTest             
private class closeAllDealsMonthAgoTest {
    static testmethod void testbatch() {
        Integer count = 200;
        Account acc = new Account(Name='Test Account');
        insert acc;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (integer i=0; i<count; i++){
            Opportunity opp = new Opportunity();
            opp.AccountId=acc.Id;
            opp.Name='My Opportunity '+i;
            opp.StageName='Qualification';
            opp.CloseDate= Date.today().addDays(-1);
            opps.add(opp);
        }
        insert opps;
        Test.setCreatedDate( opps.get( 0 ).Id, System.now().addMonths( -2 ) );
        System.assertEquals(count, opps.size());
       
       Test.startTest();
           closeAllDealsMonthAgo obj = new  closeAllDealsMonthAgo();
           Database.executeBatch(obj);
       Test.stopTest();
      
         List<Opportunity> oppUpdatedList = [SELECT Id,StageName,CloseDate FROM Opportunity];
        for(Opportunity o : oppUpdatedList){
            if(Date.Today()>o.CloseDate && (o.StageName != 'Closed Won' || o.StageName !='Closed Lost')){
                o.StageName = 'Closed Lost';
                System.assertEquals('Closed Lost', o.StageName);    
            }
        }
    }
}

I would recommend not to do the DML operation in the finish method. It should happen in the execute method. Database.stateful is not recommended for your apex class. Please let me know if you have any additional questions.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.