Defining jUnit Test cases Correctly

Posted by Epitaph on Stack Overflow See other posts from Stack Overflow or by Epitaph
Published on 2010-04-24T01:44:16Z Indexed on 2010/04/24 1:53 UTC
Read the original article Hit count: 441

Filed under:
|
|
|

I am new to Unit Testing and therefore wanted to do some practical exercise to get familiar with the jUnit framework.

I created a program that implements a String multiplier

public String multiply(String number1, String number2)

In order to test the multiplier method, I created a test suite consisting of the following test cases (with all the needed integer parsing, etc)

@Test
public class MultiplierTest {
      Multiplier multiplier = new Multiplier();
      // Test for 2 positive integers
      assertEquals("Result", 5, multiplier.multiply("5", "1"));

      // Test for 1 positive integer and 0
      assertEquals("Result", 0, multiplier.multiply("5", "0"));

      // Test for 1 positive and 1 negative integer
      assertEquals("Result", -1, multiplier.multiply("-1", "1"));

      // Test for 2 negative integers
      assertEquals("Result", 10, multiplier.multiply("-5", "-2"));

      // Test for 1 positive integer and 1 non number
      assertEquals("Result", , multiplier.multiply("x", "1"));

      // Test for 1 positive integer and 1 empty field
      assertEquals("Result", , multiplier.multiply("5", ""));

      // Test for 2 empty fields
      assertEquals("Result", , multiplier.multiply("", ""));

In a similar fashion, I can create test cases involving boundary cases (considering numbers are int values) or even imaginary values.

1) But, what should be the expected value for the last 3 test cases above? (a special number indicating error?)

2) What additional test cases did I miss?

3) Is assertEquals() method enough for testing the multiplier method or do I need other methods like assertTrue(), assertFalse(), assertSame() etc

4) Is this the RIGHT way to go about developing test cases? How am I "exactly" benefiting from this exercise?

5)What should be the ideal way to test the multiplier method?

I am pretty clueless here. If anyone can help answer these queries I'd greatly appreciate it. Thank you.

© Stack Overflow or respective owner

Related posts about junit

Related posts about java