I want to test a fix where SimpleDateFormat was used as a static class variable. I was getting
Previously my code was
public class Abc{
private static SimpleDateFormat dateformatter;
public static String method1(final Calendar calendar) {
String thePattern = "ddMMM";
dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}
public static String method2(final Calendar calendar) {
String thePattern = "ddMMyyyy";
dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}
}
For this one, I was getting below exception
java.lang.ArrayIndexOutOfBoundsException: 965
at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:454)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2333)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2248)
at java.util.Calendar.setTimeInMillis(Calendar.java:1140)
at java.util.Calendar.setTime(Calendar.java:1106)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
at java.text.DateFormat.format(DateFormat.java:336)
Now I have changed it to changed to :
public class Abc{
public static String method1(final Calendar calendar) {
String thePattern = "ddMMM";
SimpleDateFormat dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}
public static String method2(final Calendar calendar) {
String thePattern = "ddMMyyyy";
SimpleDateFormat dateformatter = new SimpleDateFormat(thePattern, Locale.US);
sPars = dateformatter.format(calendar.getTime());
//Something
}
}
How can I test that the second one is a proper fix ? Is there any way in JUnits to test multithreading ?