From 81b2ff3aa5ed7e660f0a3b1aee04957be9859945 Mon Sep 17 00:00:00 2001 From: M57 <41645758+Hy4ri@users.noreply.github.com> Date: Sat, 20 Jun 2026 23:43:46 +0300 Subject: [PATCH] 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: " 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: " 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. --- .../util/CronExpressionFormatterTest.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/src/test/java/com/m57/hermescontrol/util/CronExpressionFormatterTest.kt b/app/src/test/java/com/m57/hermescontrol/util/CronExpressionFormatterTest.kt index cc1dc68e..34816670 100644 --- a/app/src/test/java/com/m57/hermescontrol/util/CronExpressionFormatterTest.kt +++ b/app/src/test/java/com/m57/hermescontrol/util/CronExpressionFormatterTest.kt @@ -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(), 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")) + } }