If we want to assign a variable a different value based upon a condition, then there is a special syntax in Java called the ternary operator where both the logic to decide which value to use…and then doing the assignment…is all together in one instruction. However, whether or not this is OK in the code base may vary by team. I have seen teams request this not be used due to lack of familiarity within the other team members.
String result = someConditionForYourTest ? valueToUseInAssignmentIfConditionIsTrue : valueToUseInAssignmentIfConditionIsFalse;
In the example above, someConditionForYourTest could be a reference to an existing boolean variable, or it could be an equality or relational condition or test that is defined directly in the command.
When considering the snippet above for unit testing, there would be a minimum of 2 branches of logic, where someConditionForYourTest is either true or false. If more than one one test is used directly in the command, then the number of branches of logic would grow from there.

Leave a comment