I have a weird case. I am running a local test and using the Calendar class within that test.
when annotating the test class with @RunWith(AndroidJUnit4::class)
the test passes, otherwise, the test fails.
the test code doesn't include any Android environment library
here is my class
class MyDateUtils(private val calendar: Calendar) {
fun getDates(): Long{
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
return calendar.timeInMillis
}
}
and here is the test case, this one passes
@RunWith(AndroidJUnit4::class)
class MyDateUtilsTest {
private lateinit var calendar: Calendar
private lateinit var dateUtils: MyDateUtils
@Before
fun init() {
calendar = Calendar.getInstance()
calendar.timeInMillis = 1592422768000
dateUtils = MyDateUtils(calendar)
}
@Test
fun `when get dates is called with wednesday day should return sunday of the same week`() {
val expected = 1592163568000
val actual = dateUtils.getDates()
assertEquals(expected, actual)
}
}
now when I remove @RunWith(AndroidJUnit4::class)
the test fails with this error message java.lang.AssertionError: Expected :1592163568000 Actual :1592768368000
P.S the expected behavior is that the getDates()
method return Sunday within the same week. but without @RunWith(AndroidJUnit4::class)
it return the following Sunday (The next week).
This might be something to do with the default locale that might be changed when you run with AndroidJUnit4.
With different locales the first day of the week is different and that could make the tests fail or pass depending on the runner.