You need to sign in to do that
Don't have an account?
Sujit Karande
How to write test class for multiple conditions in IF statement
Hi Everyone,
Hope you're doing great. I stuck at some point where I am not aware of how we can write a tests for below piece of code from main class
I have around 30 such conditions in IF statement and for few of conditions, it is showing that Tests are passed as I checked it in Developer Console
How could we tackle this?
Thanks for your help in advance
Hope you're doing great. I stuck at some point where I am not aware of how we can write a tests for below piece of code from main class
Case obj = new Case(); if( ((obj.field1=='Yes' || obj.field2=='Yes') && checkNull(obj.field3)) || ((obj.field4=='Yes' || obj.field5=='Yes') && checkNull(obj.field6)) ) Note : checkNull() is method in the same class which checks for blank values
I have around 30 such conditions in IF statement and for few of conditions, it is showing that Tests are passed as I checked it in Developer Console
How could we tackle this?
Thanks for your help in advance
No problem. A couple of things:
I'm not sure how the class above is compiling given what's happening on lines 3-6, because I'd expect to see (for example): ...with "==" instead. "=" is an assigment operator, for setting values, while "==" is an equality operator.
Second, I don't see where "field1" is being set, and for that to work, there would need to be a field called "field1". With any sobject, you can do this kind of thing: ...but for Case, I'd expect to see API names with this syntax. So, let's assume for the moment that you can get past those, or that I'm not understanding something. To simplify what I was explaining above a little, let's assume your class looks like this: To cover it, you'd want something like this: Tests take a bit of getting used to, but this should get you started, as long as you're also using resources like the link above to make sure you understand what's happening. Best of luck, and hope this helps.
Dave (monsterloomis)
All Answers
I'm assuming you're talking about coverage here, because I'm not entirely clear from your phrasing above - you say the tests are passed, but you want to know how to test it (?)
Assuming you're looking for coverage: if there is no choice but to include complex logic like this (and I would strongly recommend confirming that that is the case, and that there are no declarative solutions available) - then in order to cover the code, you need your test class to create case records that meet the conditions of each branch in your if/else logic. There are a variety of ways to efficiently create test records, so I would do a little research there before jumping in. Regardless of elegance, your test records will need to cover each condition individually.
I would also recommend covering checkNull explicitly by passing in some values and making assertions that it returns as expected, just for completeness and because of its frequent reuse in your code. If you insert records that meet the if/else criteria as suggested, you will achieve coverage for that method, but it's generally a better practice to cover things explicitly (IMHO).
Hope that helps!
Dave
Thanks for getting back to me, that was very helpful. This is my actual class method definition
Looking forward for your response.
Best,
Sujit
No problem. A couple of things:
I'm not sure how the class above is compiling given what's happening on lines 3-6, because I'd expect to see (for example): ...with "==" instead. "=" is an assigment operator, for setting values, while "==" is an equality operator.
Second, I don't see where "field1" is being set, and for that to work, there would need to be a field called "field1". With any sobject, you can do this kind of thing: ...but for Case, I'd expect to see API names with this syntax. So, let's assume for the moment that you can get past those, or that I'm not understanding something. To simplify what I was explaining above a little, let's assume your class looks like this: To cover it, you'd want something like this: Tests take a bit of getting used to, but this should get you started, as long as you're also using resources like the link above to make sure you understand what's happening. Best of luck, and hope this helps.
Dave (monsterloomis)
All your assumption were abosulutely correct and your solution works like a charm.
You saved my job :) Appreciate your help. I will definately reach out to you in case I face any other issues
Cheers,
Sujit