🔇 Silence logger during freenet interface test
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Feb 2020 19:20:05 +0000 (20:20 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Feb 2020 19:21:53 +0000 (20:21 +0100)
src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt
src/test/kotlin/net/pterodactylus/sone/test/Logging.kt [new file with mode: 0644]

index e02cddb..81b4e04 100644 (file)
@@ -43,6 +43,10 @@ class FreenetInterfaceTest {
        @JvmField
        val expectionException: ExpectedException = ExpectedException.none()
 
+       @Rule
+       @JvmField
+       val silencedLogging = silencedLogging()
+
        @Suppress("UnstableApiUsage")
        private val eventBus = mock<EventBus>()
        private val node = mock<Node>()
diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Logging.kt b/src/test/kotlin/net/pterodactylus/sone/test/Logging.kt
new file mode 100644 (file)
index 0000000..2a2c6b7
--- /dev/null
@@ -0,0 +1,25 @@
+package net.pterodactylus.sone.test
+
+import org.junit.rules.TestRule
+import org.junit.runners.model.Statement
+import java.util.logging.Level
+import java.util.logging.Logger.getLogger
+
+/**
+ * Silences the `net.pterodactylus.sone` [logger][java.util.logging.Logger] during a test.
+ */
+fun silencedLogging() = TestRule { base, _ ->
+       object : Statement() {
+               override fun evaluate() {
+                       getLogger("net.pterodactylus.sone").let { logger ->
+                               val oldLevel = logger.level
+                               logger.level = Level.OFF
+                               try {
+                                       base.evaluate()
+                               } finally {
+                                       logger.level = oldLevel
+                               }
+                       }
+               }
+       }
+}