Skip to content

Commit

Permalink
Do not use deprecated 'ExpectedException' JUnit rule (#1621)
Browse files Browse the repository at this point in the history
The mentioned rule has been deprecated. When the tests
are compiled the warnings are visible.

Adjusted the code to use 'assertThrows()' assertion.
It is suggested by the deprecation message.

With this change the warnings are not visible
when the tests are compiled.

Signed-off-by: Patryk Wrobel <[email protected]>
  • Loading branch information
pwrobeldev authored Nov 20, 2024
1 parent 8626d00 commit 33567ae
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.gradle.api.GradleException
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
internal class GluecodiumRunnerTest {
@JvmField
@Rule
val expectedException: ExpectedException = ExpectedException.none()

@MockK lateinit var gluecodium: Gluecodium
private val defaultGluecodiumOptions = GluecodiumOptions()
private val defaultGeneratorOptions = GeneratorOptions()
Expand All @@ -62,8 +57,9 @@ internal class GluecodiumRunnerTest {
@Test
fun executeReturningFalseThrows() {
every { gluecodium.execute() } returns false
expectedException.expect(GradleException::class.java)

runner.run(defaultGluecodiumOptions, defaultGeneratorOptions)
assertThrows(GradleException::class.java) {
runner.run(defaultGluecodiumOptions, defaultGeneratorOptions)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ package com.here.gluecodium.cli
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
Expand All @@ -35,10 +35,6 @@ import java.io.PrintStream

@RunWith(JUnit4::class)
class OptionReaderTest {
@JvmField
@Rule
val expectedException: ExpectedException = ExpectedException.none()

@JvmField
@Rule
val temporaryFolder = TemporaryFolder()
Expand Down Expand Up @@ -145,8 +141,7 @@ class OptionReaderTest {
val toRead = prepareToRead("-someUnknownOption", "")

// Act, Assert
expectedException.expect(OptionReaderException::class.java)
OptionReader.read(toRead)
assertThrows(OptionReaderException::class.java) { OptionReader.read(toRead) }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,33 @@ import com.here.gluecodium.generator.cpp.TopologicalSortTestHelper.createTypeRef
import com.here.gluecodium.model.lime.LimeField
import com.here.gluecodium.model.lime.LimeStruct
import com.here.gluecodium.model.lime.LimeTypeAlias
import org.junit.Before
import org.junit.Rule
import org.junit.Assert.assertThrows
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class TopologicalSortCycleTest {
@JvmField
@Rule
val expectedException: ExpectedException = ExpectedException.none()

@Before
fun setUp() {
expectedException.expect(GluecodiumExecutionException::class.java)
}

@Test
fun cycleWithStructs() {
val fooField = LimeField(createPath("fooField"), typeRef = createTypeRef("foo"))
val barField = LimeField(createPath("barField"), typeRef = createTypeRef("bar"))
val fooStruct = LimeStruct(createPath("foo"), fields = listOf(barField))
val barStruct = LimeStruct(createPath("bar"), fields = listOf(fooField))

TopologicalSort(listOf(fooStruct, barStruct)).sort()
assertThrows(GluecodiumExecutionException::class.java) {
TopologicalSort(listOf(fooStruct, barStruct)).sort()
}
}

@Test
fun cycleWithUsings() {
val fooUsing = LimeTypeAlias(createPath("foo"), typeRef = createTypeRef("bar"))
val barUsing = LimeTypeAlias(createPath("bar"), typeRef = createTypeRef("foo"))

TopologicalSort(listOf(fooUsing, barUsing)).sort()
assertThrows(GluecodiumExecutionException::class.java) {
TopologicalSort(listOf(fooUsing, barUsing)).sort()
}
}

@Test
Expand All @@ -67,6 +60,8 @@ class TopologicalSortCycleTest {
val fooStruct = LimeStruct(createPath("foo"), fields = listOf(barField))
val barUsing = LimeTypeAlias(createPath("bar"), typeRef = createTypeRef("foo"))

TopologicalSort(listOf(fooStruct, barUsing)).sort()
assertThrows(GluecodiumExecutionException::class.java) {
TopologicalSort(listOf(fooStruct, barUsing)).sort()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ import io.mockk.unmockkAll
import io.mockk.verify
import junit.framework.TestCase.assertTrue
import org.junit.After
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.io.File
Expand All @@ -44,10 +43,6 @@ import java.nio.file.Paths

@RunWith(JUnit4::class)
class FileRemoveTest {
@JvmField
@Rule
var exception: ExpectedException = ExpectedException.none()

@MockK private lateinit var rootFile: File

@Before
Expand All @@ -70,10 +65,10 @@ class FileRemoveTest {
every { rootFile.exists() } returns false
every { rootFile.isDirectory } returns true

exception.expect(FileNotFoundException::class.java)

// Act
FileRemove(rootFile).removeFiles(emptyList())
assertThrows(FileNotFoundException::class.java) {
FileRemove(rootFile).removeFiles(emptyList())
}
}

@Test
Expand All @@ -83,10 +78,10 @@ class FileRemoveTest {
every { rootFile.exists() } returns true
every { rootFile.isDirectory } returns false

exception.expect(FileNotFoundException::class.java)

// Act
FileRemove(rootFile).removeFiles(emptyList())
assertThrows(FileNotFoundException::class.java) {
FileRemove(rootFile).removeFiles(emptyList())
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@
package com.here.gluecodium.model.lime

import com.here.gluecodium.model.lime.LimePath.Companion.EMPTY_PATH
import org.junit.Rule
import org.junit.Assert.assertThrows
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class LimeAmbiguityResolverTest {
@JvmField
@Rule
val exception: ExpectedException = ExpectedException.none()

private val fooPath = LimePath(listOf("foo"), emptyList())
private val limeElement = object : LimeNamedElement(EMPTY_PATH) {}

Expand All @@ -51,9 +46,9 @@ class LimeAmbiguityResolverTest {

@Test
fun resolveWithNoPaths() {
exception.expect(LimeModelLoaderException::class.java)

doResolve()
assertThrows(LimeModelLoaderException::class.java) {
doResolve()
}
}

@Test
Expand All @@ -80,49 +75,54 @@ class LimeAmbiguityResolverTest {

@Test
fun resolveWithParentPathEmptyMap() {
exception.expect(LimeModelLoaderException::class.java)
referenceMap.clear()
parentPaths += fooPath

doResolve()
assertThrows(LimeModelLoaderException::class.java) {
doResolve()
}
}

@Test
fun resolveWithImportEmptyMap() {
exception.expect(LimeModelLoaderException::class.java)
referenceMap.clear()
imports += fooPath.child("Bar")

doResolve()
assertThrows(LimeModelLoaderException::class.java) {
doResolve()
}
}

@Test
fun resolveWithAmbiguousImports() {
exception.expect(LimeModelLoaderException::class.java)
imports += fooPath.child("Bar")
imports += fooPath.child("Buzz").child("Bar")
referenceMap["foo.Buzz.Bar"] = object : LimeType(EMPTY_PATH) {}

doResolve()
assertThrows(LimeModelLoaderException::class.java) {
doResolve()
}
}

@Test
fun resolveWithAmbiguousLocalObjects() {
exception.expect(LimeModelLoaderException::class.java)
parentPaths += fooPath
parentPaths += fooPath.child("Buzz")
referenceMap["foo.Buzz.Bar"] = object : LimeNamedElement(EMPTY_PATH) {}

doResolve()
assertThrows(LimeModelLoaderException::class.java) {
doResolve()
}
}

@Test
fun resolveWithCombinedAmbiguity() {
exception.expect(LimeModelLoaderException::class.java)
parentPaths += fooPath
imports += fooPath.child("Buzz").child("Bar")
referenceMap["foo.Buzz.Bar"] = object : LimeNamedElement(EMPTY_PATH) {}

doResolve()
assertThrows(LimeModelLoaderException::class.java) {
doResolve()
}
}
}

0 comments on commit 33567ae

Please sign in to comment.