You need to sign in to do that
Don't have an account?
ManishKSingh
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.
if(Date.today().month()==1){
}
Any help will be greatly appreciated.
Just update your code like below
Let us know if this will help you
All Answers
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
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
Your suggestion help me to get code coverage for my 'if' condition, but now below code is covered.
compareMonth = 1;
Thanks
Manish
Just update your code like below
Let us know if this will help you
I am get 100% code coverage for my class.
Only possible way is to re-work a bit on your class/ method. Below would be my suggestions
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