Unparseable date Exception on Unit Testing via Mockito

56 Views Asked by At

In my case, i am in need of get the string date as format i prefer , below is the exact code i am using to get the return date as string

Below is the Test Function:

@Test
fun `get formatted server date`() {

    assertEquals(HelperFunctions.getFormattedServerDate("2024-01-05T06:00:00", "yyyy-MM-dd'T'HH:mm:ssZZZZZ" ),"Fri Jan 05 11:30:00 GMT+05:30 2024")
}

And the getFormattedServerDate function is below as

fun getFormattedServerDate(date: String, dateFormat: String): Date? {
            val dateFormatter = SimpleDateFormat(dateFormat, Locale.US)
            dateFormatter.timeZone = TimeZone.getTimeZone("GMT")
            return dateFormatter.parse(date)
        }

On a run of the above code with input date as 2024-01-05T06:00:00 and format as yyyy-MM-dd'T'HH:mm:ssZZZZZ i get the output as Fri Jan 05 11:30:00 GMT+05:30 2024 - Note: This happens in the realtime not in the Test case run Exception:

Unparseable date: "2024-01-05T06:00:00+00:00"
java.text.ParseException: Unparseable date: "2024-01-05T06:00:00+00:00"

When i am executing the Test case i am facing the unparse exception, but if i try with the releatime code i dont see any issues, it works perfect in conversion

0

There are 0 best solutions below