Add error path coverage for CronExpressionFormatter (#182)

* test: add error path coverage for CronExpressionFormatter

Added a new test case to cover the fallback code path when cron parsing throws an exception. This test ensures the formatting correctly defaults to returning "Raw: <schedule>" when an invalid string like '0 12 1 15 *' or '0 9 * * 8' is passed.

It also introduces mockk integration to mock android.util.Log which prevents RuntimeExceptions from being thrown during local JUnit tests when logging errors in the catch block.

* test: add error path coverage for CronExpressionFormatter and fix ktlint issues

Added a new test case to cover the fallback code path when cron parsing throws an exception. This test ensures the formatting correctly defaults to returning "Raw: <schedule>" when an invalid string like '0 12 1 15 *' or '0 9 * * 8' is passed.

It also introduces mockk integration to mock android.util.Log which prevents RuntimeExceptions from being thrown during local JUnit tests when logging errors in the catch block.

Also removed trailing whitespace and an empty line at the start of the class to fix ktlint checking in CI.
This commit is contained in:
M57 authored and GitHub committed 2026-06-20 23:43:46 +03:00
1 parent 34af34af6d
commit 81b2ff3aa5
1 file changed
+26
@@ -1,9 +1,26 @@
package com.m57.hermescontrol.util
import android.util.Log
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
class CronExpressionFormatterTest {
@Before
fun setup() {
mockkStatic(Log::class)
every { Log.w(any(), any<String>(), any()) } returns 0
}
@After
fun teardown() {
unmockkAll()
}
@Test
fun testCronToHumanReadable() {
assertEquals("At 09:00 and 21:00 every day", CronExpressionFormatter.cronToHumanReadable("0 9,21 * * *"))
@@ -22,4 +39,13 @@ class CronExpressionFormatterTest {
assertEquals("invalid cron string", CronExpressionFormatter.cronToHumanReadable("invalid cron string"))
assertEquals("0 9 * *", CronExpressionFormatter.cronToHumanReadable("0 9 * *"))
}
@Test
fun testExceptionFallback() {
// This will cause an IndexOutOfBoundsException because 15 is out of bounds for monthNames
assertEquals("Raw: 0 12 1 15 *", CronExpressionFormatter.cronToHumanReadable("0 12 1 15 *"))
// This will cause an IndexOutOfBoundsException because 8 is out of bounds for dowNames
assertEquals("Raw: 0 9 * * 8", CronExpressionFormatter.cronToHumanReadable("0 9 * * 8"))
}
}