From 2d96d9356f946cf660e2123883a6d754ee139fef Mon Sep 17 00:00:00 2001 From: Angus Holder Date: Fri, 27 Mar 2026 16:43:33 +0000 Subject: [PATCH 1/2] Catch Throwable in LogcatWriter, for unit tests --- .../androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt b/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt index f027a521..d02f98b2 100644 --- a/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt +++ b/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt @@ -45,7 +45,10 @@ class LogcatWriter(private val messageStringFormatter: MessageStringFormatter = ) } } - } catch (_: Exception) { + } catch (_: Throwable) { + // Catches "RuntimeException: Method d in android.util.Log not mocked" + // or "UnsatisfiedLinkError: 'int android.util.Log.println_native(int, int, java.lang.String, java.lang.String)'" + // when running unit tests. testWriter.log(severity, message, tag, throwable) } } From e773d894b9c430daacdcb604e264fdfab5bd0aa1 Mon Sep 17 00:00:00 2001 From: Gustavo Fao Valvassori Date: Fri, 31 Jul 2026 16:38:34 -0300 Subject: [PATCH 2/2] updated catch to only run for the needed errors --- .../kotlin/co/touchlab/kermit/LogcatWriter.kt | 7 ++-- .../co/touchlab/kermit/LogcatWriterTest.kt | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 kermit-core/src/androidUnitTest/kotlin/co/touchlab/kermit/LogcatWriterTest.kt diff --git a/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt b/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt index d02f98b2..1c37417f 100644 --- a/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt +++ b/kermit-core/src/androidMain/kotlin/co/touchlab/kermit/LogcatWriter.kt @@ -45,10 +45,9 @@ class LogcatWriter(private val messageStringFormatter: MessageStringFormatter = ) } } - } catch (_: Throwable) { - // Catches "RuntimeException: Method d in android.util.Log not mocked" - // or "UnsatisfiedLinkError: 'int android.util.Log.println_native(int, int, java.lang.String, java.lang.String)'" - // when running unit tests. + } catch (_: RuntimeException) { + testWriter.log(severity, message, tag, throwable) + } catch (_: UnsatisfiedLinkError) { testWriter.log(severity, message, tag, throwable) } } diff --git a/kermit-core/src/androidUnitTest/kotlin/co/touchlab/kermit/LogcatWriterTest.kt b/kermit-core/src/androidUnitTest/kotlin/co/touchlab/kermit/LogcatWriterTest.kt new file mode 100644 index 00000000..37bddae9 --- /dev/null +++ b/kermit-core/src/androidUnitTest/kotlin/co/touchlab/kermit/LogcatWriterTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 Touchlab + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package co.touchlab.kermit + +import kotlin.test.Test +import kotlin.test.fail + +/** + * The [LogcatLoggerTest] class can run without failing as it uses the roboeletric runner, which produces the stubs. + * In this sample, we run it without it to simulate the real user experience when they are not using roboeletric. + */ +class LogcatWriterTest { + private val testConfig = TestConfig( + minSeverity = Severity.Verbose, + logWriterList = listOf(LogcatWriter()), + ) + + @Test + fun logsToLogcat_withoutMocks_doNotCrash() { + val logger = BaseLogger(testConfig) + + try { + logger.logBlock(Severity.Debug, "tag", null) { "Message" } + } catch (e: Throwable) { + fail("Logcat call should crash in Android UnitTest mode", e) + } + } +}