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
symantecAPsymantecAP 

Help with testing IF Condition

Hi All

 

I have the following code and i am not able to test IF Condition in my code.

 

Here is my class and test class so far

global class updateOpportunityStage implements Database.Batchable<sObject>,Schedulable{
global string query ;

global updateOpportunityStage(){

Query = 'Select Id,BigMachines__Status__c,BigMachines__Is_Primary__c,BigMachines__Opportunity__c    from BigMachines__Quote__c where BigMachines__Is_Primary__c = true and BigMachines__Status__c like \'%unison%\' ';

}

global database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);    
}
    global void execute(SchedulableContext SC){
        integer bSize;
        integer Interval;
        try{
            Apex_Jobs_Settings__c aJob = [select id, name, batch_size__c, Run_Schedule_Interval__c, Isactive__c from Apex_Jobs_Settings__c where name = 'ContactsMerge' limit 1];
            if( aJob.IsActive__c = true){
                bSize = integer.valueOf(aJob.Batch_Size__c);
                Interval = integer.valueOf(aJob.Run_Schedule_Interval__c);
            }  
            else{
      
      
            }
        }
        catch(Exception ex){
        system.debug('ERROR: '+ ex);
        bSize=150;
        interval = 30;
    }    
        updateOpportunityStage stg = new updateOpportunityStage();
        DateTime todayMin = Datetime.now();
        DateTime today = todayMin.addMinutes(Interval);
        
        String seconds = '0';
        Integer minutes = today.minute();
        Integer hours = today.hour(); 
        Integer dayOfMonth = today.day(); 
        Integer month = today.month(); 
        Integer milisec = today.millisecond();
        String dayOfWeek = '?'; 
        Integer year = today.year(); 

        String sch = seconds + ' ' + minutes + ' ' + hours + ' ' + dayOfMonth + ' ' + month + ' ' + dayOfWeek + ' ' + year;
     
        String j = 'New Job:'+hours+':'+minutes+':'+seconds+':'+milisec;

        system.schedule(j, sch, stg);
       
        database.executebatch(stg, bSize);
        
     }

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

     
    Set<id> liOppIds = new Set<id>();

for(sObject s : scope){

BigMachines__Quote__c quote = (BigMachines__Quote__c)s;
System.debug('Adil'+quote);
if(quote.BigMachines__Status__c.contains('Unison') && quote.BigMachines__Is_Primary__c == true)
liOppIds.add(quote.BigMachines__Opportunity__c);

}


//query all the opportunities in a single query
List<Opportunity> opp = new List<Opportunity>();
opp = [select id, StageName from Opportunity where id in :liOppIds and stagename != 'Closed Won' and  CloseDate >= Today];
for ( Opportunity opps : opp)
{
opps.StageName = 'Closed Won' ; 
}
//update all opportunities in a single DML
if(opp.size() > 0)
update opp;
 
    }
  global void finish(Database.BatchableContext BC){}  

}

 Test class is as follows

 

@isTest

Private class updateOpportunityStage_Test
{
static testMethod void Testclass(){

Opportunity Opp = new Opportunity(Name = 'Test',StageName = 'Closed Won',CloseDate= date.today());

insert opp;



BigMachines__Quote__c bmq = new BigMachines__Quote__c(BigMachines__Status__c = '%Unison%');

insert bmq;
  Apex_Jobs_Settings__c aJob = new   Apex_Jobs_Settings__c(Name ='test',batch_size__c=150,Run_Schedule_Interval__c=30,Isactive__c=true);

insert ajob;



  Test.startTest();  
// Schedule the job
            updateOpportunityStage stg = new updateOpportunityStage();

        
        ID batchprocessid = Database.executeBatch(stg,150);
              updateOpportunityStage stg1 = new updateOpportunityStage();
   String sch = '0 0 6 13 2 ?';        
     system.schedule('Schedule Test', sch, stg1);
 Test.stopTest();

}


}

 Thanks

Adil

bob_buzzardbob_buzzard

Can you identify which if condition you are referring to - looks like there's three in there.

VishwanathVishwanath

Hi,

 

If condition is cover only when the required conditions is meet in your test case

in your test case your checking

if( aJob.IsActive__c = true)

 

so insert  Apex_Jobs_Settings__c with the field IsActive is 'true'

 

 

Hope it wil help you

symantecAPsymantecAP

Yes I am checking for  the below IF conditions. and also insrted Apex_Jobs_Settings__c with the field IsActive is 'true' in my test code.

 

The actual redlines are on following codes.

 

if( aJob.IsActive__c = true){
                bSize = integer.valueOf(aJob.Batch_Size__c);
                Interval = integer.valueOf(aJob.Run_Schedule_Interval__c);

opps.StageName = 'Closed Won' ; 
}
//update all opportunities in a single DML
if(opp.size() > 0)
update opp;

 Thanks for the help

bob_buzzardbob_buzzard

Opp size won't be greater than zero as you have inserted one opportunity with a stage of closed won, but your query is:

 

opp = [select id, StageName from Opportunity where id in :liOppIds and stagename != 'Closed Won' and  CloseDate >= Today];
symantecAPsymantecAP

I just changed opp.stagename='trial' and that covered the code to 93%.

symantecAPsymantecAP

I still am struggling with the other IF where.Is there any approach for this IF to be covered

 

if( aJob.IsActive__c = true){
                bSize = integer.valueOf(aJob.Batch_Size__c);
                Interval = integer.valueOf(aJob.Run_Schedule_Interval__c);
atulajawaleatulajawale

Please change the aJob.name to 'ContactsMerge' in test script.