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)
public class MultiplierTest {
@Test
public void testMultiply() {
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.
1) But, what should be the expected value for the last 3 test cases above? (a special number indicating error?)
As explained by the other answerers, it depends on the interface contract of your multiplier. You should think through how you (or its clients, in general) are supposed to use it, what should happen in case of specific errors or extreme cases, etc. In Java, the convention is to throw an exception in such cases.
2) What additional test cases did I miss?
A couple of cases which come to my mind:
// test commutativity
assertEquals("0", multiplier.multiply("0", "5"));
assertEquals("-1", multiplier.multiply("1", "-1"));
assertEquals("149645", multiplier.multiply("173", "865"));
assertEquals("149645", multiplier.multiply("865", "173"));
// test some more unusual cases of multiplying with 0
assertEquals("0", multiplier.multiply("-5", "0"));
assertEquals("0", multiplier.multiply("0", "-0"));
// test with numbers starting with '+'
assertEquals("368", multiplier.multiply("+23", "+16"));
assertEquals("-368", multiplier.multiply("-23", "+16"));
// test multiplying huge values without overflow
assertEquals("18446744073709551616", multiplier.multiply("4294967296", "4294967296"));
assertEquals("18446744073709551616", multiplier.multiply("-4294967296", "-4294967296"));
3) Is assertEquals() method enough for testing the multiplier method or do I need other methods like assertTrue(), assertFalse(), assertSame() etc
In this case all you need is to compare two values for equality. In other tests you may need different kinds of asserts.
4) Is this the RIGHT way to go about developing test cases? How am I "exactly" benefiting from this exercise?
There is no single "right" way to unit testing. What comes closest is probably test driven development, which is recommended by many (including myself) if you write your code from scratch.
Your benefit from this exercise is probably that you got familiar with JUnit and tried the "tester hat" on for a while.
5)What should be the ideal way to test the multiplier method?
How is this question different from the previous one?