Je souhaite tester un correctif où SimpleDateFormat a été utilisé en tant que variable de classe statique. Je recevaisTest de plusieurs threads appel pour SimpleDateFormat
Auparavant mon code était
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
}
}
Pour celui-ci, je devenais ci-dessous 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)
Maintenant, je l'ai changé à changé:
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
}
}
Comment puis-je tester que le second est un correctif approprié? Existe-t-il un moyen pour JUnits de tester le multithreading?
Voir aussi https://stackoverflow.com/questions/12159/how-should-i-unit- test-threaded-code – Raedwald