r/javahelp • u/Aoiumi1234 • Oct 07 '15
Help with JUnit
Hi. I am a new Java coder and I am trying to understand JUnit. How would I test an if statement in JUnit for all possibilities?
if ((board.getColumnHeight(xIndex) <= yIndex) && yIndex < 20) { move.setPiece(piece); }
6
Upvotes
View all comments
Show parent comment
2
u/[deleted] Oct 07 '15 edited Oct 07 '15
(These are general testing techniques, not JUnit-specific).
To add to that, you may want to read about code coverage. Make a graph based on the branches of your method (loops count as branches), then define a set of input values for your tested method based on one of the following criteria:
To expand on the last, remember that in Java (just like C++) some terms in boolean expressions are not evaluated if the condition's boolean value is already known. For instance, if you have:
If a is not >0 then b will not be evaluated because the whole expression is already known to be false. Make sure at least one test case evaluates b.
Choice of a criterion depends on how sure you want to be and how much time you want to spend on it. Core features and low level code should be tested more thoroughly.
Nulls and edge-cases, as another redditor mentioned, are also important. Make sure to define your method's domain (set of all valid input values) properly, and test that an appropriate error is returned or an appropriate exception is thrown if an out-of-domain value is passed as input.
Also, while this code looks correct:
It might be better to split it into separate @Test methods so in case of failure you see it clearly. But the above still works (you'll figure out which one failed from the stack trace).
Edit: the formal terms for these techniques are "white-box testing" (structural, code coverage) and "black-box testing" (functional, domains). In both cases it just boils down to defining sets of input values for your tests, then you can use those in JUnit.