I’m testing a WeekConverter for Xalan use and wondering what is my test exactly doing. 😀
Having the following test method:
@Test(expected = IllegalArgumentException.class)
public void testConvertTwoDigitYearWithWrongInput() {
WeekConverter weekConverter = new WeekConverter(WeekConverter.Strategy.TWO_DIGIT_YEAR);
//wrong or empty inputs
assertEquals("0", weekConverter.convert(""));
assertEquals("0", weekConverter.convert("abcdefgh"));
}
Will this test expect an exception for all asserts, or only for the first assert? If only the first, which would mean that I have to create a test method for each assert, although I’m expecting the same exception in both cases. Can someone confirm my example here, please?
I also have a test for null, which yields a NullPointerException. The soft validation is the following:
if (inputDate == null) {
do something and throw NullPointerexception
} else if (inputDate.isEmpty()) {
do something and throw IllegalArgumentException, since inputDate is not really null
} else if (inputDate.matches(regex)) {
go futher and convert
} else {
do something and throw IllegalArgumentException, since inputDate does not match regex
}
Therefore the one test method expecting IllegalArgumentException with two asserts. But it’s obvious that I need two different test methods, not only to respect functionality of JUnit , but also that I expect a throw from two different states.
You can break your method into multiple methods, but if you have many input samples it would be inconvenient.
You can use the following approach instead: