To make sure that your method is bullet-proof, you should test every possible way to call it that you can think of, including with a
null
argument, unless you place a dire warning on the code that says:DO NOT EVER CALL THIS CODE WITH A
null
ARGUMENT.If it is a utility class with only static methods, the no-argument constructor should be
private
and the class should befinal
, to emphasize this fact. Then maybe emma will leave you alone, especially if this constructor is empty.
jUnit and DateFormat (EclEmma code coverage)
質問
I'm new to jUnit and I'm trying to deep more my knowledge about it. I searched on the web but didn't found anything to solve a couple of doubts.
This is the code:
public class StringConverter {
public static String formatDate(Date date) {
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
return sdf.format(date);
}
}
This is the jUnit4 Test Case:
public class StringConverterTest {
@Test
public void testFormatDate() {
Calendar calendar = new GregorianCalendar(2013, 02, 13, 8, 30, 00);
assertEquals("13/03/2013 08:30:00", StringConverter.formatDate(calendar.getTime()));
}
}
The TestCase runs correctly without any problems but i have 2 simple questions/problems:
1) It's correct to test only the correct functionality of the method or should i test also null
values and/or any particular exception?
2) When i run the code coverage with EclEmma it gives me 75% code coverage because the test case is not testing the constructor of the StringConverter
class. Testing the StringConverter
class constructor is not in my plan since the StringConverter
class is a util class, so it won't be instanced. There is a way to exclude this from the code coverage?
Any advice wil be appreciated. Thanks.
解決