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
ManishKSinghManishKSingh 

How i get code coverage for 'if' condition.

Can anyone help me to get code coverage for below 'if' condition..

if(Date.today().month()==1){ 
}

Any help will be greatly appreciated.
Best Answer chosen by ManishKSingh
Amit Chaudhary 8Amit Chaudhary 8
+1 Gaurav,

Just update your code like below
Integer compareMonth = 1;

if(Test.isRunningTest())
    compareMonth = Date.today().month();

if(Date.today().month()== compareMonth )
{
	
}



Let us know if this will help you

All Answers

Vasani ParthVasani Parth
Manish,
You need to create two test method in your test class. In first method add conditions so that your if condition will be satisfied. In second method add conditions so that if condition will not be satisfied and else part will be executed. So you will have code coverage for if part as well as else part.

Please mark this as the best answer if this helps
GauravGargGauravGarg
Hi Manish,

You have used hardcode month value "1", as Date.today().month() will return the current month number i.e. 7. The If condition can be cover only in Month of jan. 

Or you can change the code like mentioned below:
if(Test.isRunningTest())
   Integer compareMonth = Date.today().month();
else
  compareMonth = 1;

if(Date.today().month()== compareMonth ){ 
}

Let me know if you still face some issues, or you can contact me: email: gauravgarg.nmims@gmail.com, skype: gaurav62990

Thanks,
Gaurav
ManishKSinghManishKSingh
Thanks for replying gaurav.

Your suggestion help me to get code coverage for my 'if' condition, but now below code is covered.

compareMonth = 1;

Thanks
Manish
Amit Chaudhary 8Amit Chaudhary 8
+1 Gaurav,

Just update your code like below
Integer compareMonth = 1;

if(Test.isRunningTest())
    compareMonth = Date.today().month();

if(Date.today().month()== compareMonth )
{
	
}



Let us know if this will help you
This was selected as the best answer
ManishKSinghManishKSingh
Thanks Amit.

I am get 100% code coverage for my class.
 
VineetKumarVineetKumar
Not possible, it will only cover this line in the month of January.
Only possible way is to re-work a bit on your class/ method. Below would be my suggestions
  • Instead of directly using Date.Today().month, you should have assigned this value in a variable, put this variable as public and in your test class you could have assigned value to this variable and manipulated in your actual code.
  • Or do something like this in your code.
  • Date useThisDate;
    if(Test.isRunningTest()){
        useThisDate = date.parse('01/01/2016');
    }else{
        useThisDate = Date.today();
    }
    
    if(useThisDate .month()==1){ 
    .
    .
    .
    .
    }
    
  • Or you would have created a method (that would contains this logic) and pass the required date value to it.
Veera7Veera7
Hi Manish,

You have to use Test.isRunningTest() to cover this senario. 

date myDate = date.newInstance(2016, 1, 21);
if(Date.today().month()==1 || ( Test.isRunningTest()
            && myDate!= null && myDate.month()== 1 )) {
    //Your logic
}

thanks,
Veera