As you can see here, I wrote the methods public static int romanToArabic(String number)
and public static String arabicToRoman(int number)
to convert roman numbers to arabic numbers and vice versa.
I now wrote a unit-testing-class to test these methods. It is my first attempt to write a (real) unit test, so I would like to know your opinion.
Here's the code:
import org.junit.Test;
import static org.junit.Assert.*;
public class TestCases {
@Test
public void testRomanToArabic() {
String[] input = {null, "", "I", "V", "XXXIII", "DCCXLVII", "CMXXIX", "MCCXXXII", "MMMCMXCIX", "MMMMXI", "KMXI",
"VX", "VL", "VC", "VD", "VM", "LC", "LD", "LM", "DM", "IL", "IC", "ID", "IM", "XD", "XM", "CXLV", "MIXI", "IXI", "MXIII", "MMMM", "IIII"};
int[] expectedOutput = {-1, -1, 1, 5, 33, 747, 929, 1232, 3999, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 145, -1, -1, 1013, -1, -1};
for(int i = 0; i < input.length; i++) {
int k = RomanNumbers.romanToArabic(input[i]);
try {
assertEquals(k, expectedOutput[i]);
}
catch(AssertionError assertFailed) {
System.out.println("Test failed!");
System.out.println("Input: " + input[i]);
System.out.println("Output: " + k + "| Expected: " + expectedOutput[i]);
}
}
}
@Test
public void testArabicToRoman() {
int[] input = {-1, 1, 5, 33, 747, 929, 1232, 3999, 4000};
String[] expectedOutput = {"Error", "I", "V", "XXXIII", "DCCXLVII", "CMXXIX", "MCCXXXII", "MMMCMXCIX", "Error"};
for(int i = 0; i < input.length; i++) {
String k = RomanNumbers.arabicToRoman(input[i]);
try {
assertEquals(k, expectedOutput[i]);
}
catch(AssertionError assertFailed) {
System.out.println("Test failed!");
System.out.println("Input: " + input[i]);
System.out.println("Output: " + k + "| Expected: " + expectedOutput[i]);
}
}
}
}
Is this the way to go, or is it considered bad practice to put more than one test in a method? Do you have any other suggestions on improving the code?