JUnit 4 Test Case

640 views Asked by At

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() {

        }

        }
3

There are 3 answers

3
GhostCat On

Something to get you started ...

First an "extended" version that is maybe more helpful to "newbies":

@Test
public void testXandYEqual() {
  // arrange
  int x=0;
  int y=0;
  int anyValueBeingIgnored=0;
  // act
  int result = ThatClass.computeValue(x, y, anyValueBeingIgnored);
  // assert
  assertThat(result, is(1));
}

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:

@Test
public void testXandYEqual() {
  assertThat(ThatClass.computeValue(0, 0, 1), is(1));
}

( 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.

0
J-J On

Something like this.

    @Test
    public void testcomputeValueWithXYandZAsZero() {

        int result = YourClass.computeValue(0, 0, 0);

        Assert.assertEquals(1, result);
    }

Make sure that you write test cases with different set of inputs so that all the branches of the static method is covered.

You can use plugins like EclEmma to check the coverage of your tests. http://www.eclemma.org/

0
Nurjan On

Supposed your method to test is located in the class Stackoverflow. You need a test class named StackoverflowTest. And this is how it might look:

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author someAuthor
 */
public class StackoverflowTest {

    public StackoverflowTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}

    // Here you test your method computeValue()
    // You cover all three cases in the computeValue method

    @Test
    public void computeValueTest() {
        assertTrue(3 == computeValue(2, 2, 0));
        assertTrue(4 == computeValue(3, 2, 0));
        assertTrue(200 == computeValue(1,2,200));
    }
}