I need help on creating JUnit 4 Test Case for this code.
public static int computeValue(int x, int y, int z)
{
int value = 0;
if (x == y)
value = x + 1;
else if ((x > y) && (z == 0))
value = y + 2;
else
value = z;
return value;
}
Edited
I want something like this to test if else statement
public class TestingTest {
@Test
public void testComputeValueTCXX() {
}
…
@Test
public void testComputeValueTCXX() {
}
}
Something to get you started ...
First an "extended" version that is maybe more helpful to "newbies":
The above tests the first case of those cascade of ifs; where assertThat is one of the many JUnit asserts; and is() is a hamcrest matcher method. Besides, this is how I would write that testcase:
( the main difference is: for me, a unit test should not contain any information that I dont need; in that sense: I want it to be as pure, short, concise as possible )
Basically you want to write up different tests in order to cover all paths going through your logic. You could use one of the many existing coverage tools to ensure that you covered all paths.
Alternatively, you could also look into parameterized tests. Meaning: instead of creating a lot of test methods where each one just calls your real method with different arguments, you would put all those different "call parameters" into a table; and then JUnit takes all data from that table and uses that when invoking that "target" method under test.