♻️ Replace deprecated ExpectedException @Rule with assertThrows
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 6 Apr 2025 08:03:00 +0000 (10:03 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 6 Apr 2025 16:19:37 +0000 (18:19 +0200)
src/test/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnectorTest.kt
src/test/kotlin/net/pterodactylus/sone/test/TestUtils.kt

index 0c68bfe..575218b 100644 (file)
@@ -29,21 +29,19 @@ import net.pterodactylus.sone.freenet.*
 import org.hamcrest.MatcherAssert.*
 import org.hamcrest.Matchers.*
 import org.junit.*
-import org.junit.rules.*
 import kotlin.concurrent.*
+import net.pterodactylus.sone.test.assertThrows
 
 class FredPluginConnectorTest {
 
-       @Rule
-       @JvmField
-       val expectedException = ExpectedException.none()!!
-
        @Test
-       fun `connector throws exception if plugin can not be found`() = runBlocking {
+       fun `connector throws exception if plugin can not be found`() {
                val pluginConnector = FredPluginConnector(pluginRespiratorFacade)
-               expectedException.expect(PluginException::class.java)
+               assertThrows<PluginException> {
+                       runBlocking {
                pluginConnector.sendRequest("wrong.plugin", requestFields, requestData)
-               Unit
+                       }
+               }
        }
 
        @Test
index 2b9854d..de5931c 100644 (file)
@@ -1,9 +1,9 @@
 package net.pterodactylus.sone.test
 
-import org.junit.rules.ExpectedException
 import java.io.InputStreamReader
 import java.nio.charset.Charset
 import java.nio.charset.StandardCharsets.UTF_8
+import org.junit.Assert.assertThrows
 import sun.misc.Unsafe
 
 inline fun <reified O : Any> setField(instance: O, name: String, value: Any?) {
@@ -15,7 +15,7 @@ inline fun <reified O : Any> setField(instance: O, name: String, value: Any?) {
        unsafe.putObject(instance, offset, value)
 }
 
-inline fun <reified T : Throwable> ExpectedException.expect() = expect(T::class.java)
+inline fun <reified T : Throwable> assertThrows(noinline block: () -> Unit): T = assertThrows(T::class.java, block)
 
 fun resource(name: String, resourceClass: Class<*>, encoding: Charset = UTF_8): String? = resourceClass.getResourceAsStream(name)?.use { inputStream ->
        InputStreamReader(inputStream, encoding).use { inputStreamReader ->