-5

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

        }

        }
Syy
  • 11
  • 3
  • 3
    before this have you ever written a test? If not you shouldn't start learning by posting question but following some basic tutorials. – Mritunjay Dec 22 '16 at 09:56
  • Please add a complete JavaDoc to your method and describe what this method should do and what not. – Tobias Otto Dec 22 '16 at 10:06

3 Answers3

5

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • *"a unit test should not contain any information that I dont need;"* The types and meanings of input parameters and the type of the returned value in fact *are* important information an unittest should contain. Think of Unittest as *specification* rather that a simple prove. – Timothy Truckle Dec 22 '16 at 10:46
  • Probably the reasonable solution would some middle ground; for example by having those three parameters as explicit values. But those // comments do not have any value ... when you have created hundreds of tests before. – GhostCat Dec 22 '16 at 11:37
  • *"But those // comments do not have any value"* sure, they are there for the OP who is new to unittesting. on the othere hand you should visually separate this 3 parts of a UT by at least a single empty line... – Timothy Truckle Dec 22 '16 at 11:42
1

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/

Jobin
  • 5,610
  • 5
  • 38
  • 53
0

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));
    }
}
Nurjan
  • 5,889
  • 5
  • 34
  • 54