Skip to content

Commit b262de5

Browse files
bedaHovorkaCopilot
andauthored
Replace try-catch with assertFailure in ExampleLoadingTest (#51, #50, #52, #49, #53, #55)
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
1 parent 84faab4 commit b262de5

1 file changed

Lines changed: 30 additions & 38 deletions

File tree

src/test/kotlin/cz/vutbr/fit/interlockSim/ExampleLoadingTest.kt

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
*/
1010
package cz.vutbr.fit.interlockSim
1111

12+
import assertk.assertFailure
1213
import assertk.assertThat
14+
import assertk.assertions.cause
15+
import assertk.assertions.contains
1316
import assertk.assertions.hasSize
1417
import assertk.assertions.isEqualTo
1518
import assertk.assertions.isFalse
19+
import assertk.assertions.isInstanceOf
1620
import assertk.assertions.isNotNull
1721
import assertk.assertions.isTrue
22+
import assertk.assertions.message
1823
import cz.vutbr.fit.interlockSim.context.ContextCreationException
1924
import cz.vutbr.fit.interlockSim.context.SimulationContextFactory
2025
import cz.vutbr.fit.interlockSim.xml.XMLContextFactory
@@ -201,13 +206,11 @@ class ExampleLoadingTest {
201206
val unknownExampleName = "nonExistentExample"
202207

203208
// Act & Assert
204-
// TODO: Replace try-catch with assertFailure - https://github.com/bedaHovorka/interlockSim/issues/44
205-
try {
209+
assertFailure {
206210
mainClass.getMethod(unknownExampleName, SimulationContextFactory::class.java, Array<String>::class.java)
207-
throw AssertionError("Should have thrown NoSuchMethodException for unknown example")
208-
} catch (e: NoSuchMethodException) {
209-
assertThat(e.message).isNotNull()
210-
}
211+
}.isInstanceOf<NoSuchMethodException>()
212+
.message()
213+
.isNotNull()
211214
}
212215

213216
/**
@@ -240,16 +243,13 @@ class ExampleLoadingTest {
240243
val insufficientArgs = arrayOf("example") // Missing end time
241244

242245
// Act & Assert
243-
// TODO: Replace try-catch with assertFailure - https://github.com/bedaHovorka/interlockSim/issues/45
244-
try {
245-
val method = mainClass.getMethod("shuntingLoop", SimulationContextFactory::class.java, Array<String>::class.java)
246+
val method = mainClass.getMethod("shuntingLoop", SimulationContextFactory::class.java, Array<String>::class.java)
247+
assertFailure {
246248
method.invoke(main, factory, insufficientArgs)
247-
throw AssertionError("Should have thrown ContextCreationException for missing end time")
248-
} catch (e: InvocationTargetException) {
249-
val cause = e.cause
250-
assertThat(cause).isNotNull()
251-
assertThat(cause is ContextCreationException).isTrue()
252-
}
249+
}.isInstanceOf<InvocationTargetException>()
250+
.cause()
251+
.isNotNull()
252+
.isInstanceOf<ContextCreationException>()
253253
}
254254

255255
/**
@@ -265,16 +265,13 @@ class ExampleLoadingTest {
265265
val invalidArgs = arrayOf("example", "shuntingLoop", "notANumber")
266266

267267
// Act & Assert
268-
// TODO: Replace try-catch with assertFailure - https://github.com/bedaHovorka/interlockSim/issues/46
269-
try {
270-
val method = mainClass.getMethod("shuntingLoop", SimulationContextFactory::class.java, Array<String>::class.java)
268+
val method = mainClass.getMethod("shuntingLoop", SimulationContextFactory::class.java, Array<String>::class.java)
269+
assertFailure {
271270
method.invoke(main, factory, invalidArgs)
272-
throw AssertionError("Should have thrown NumberFormatException for non-numeric end time")
273-
} catch (e: InvocationTargetException) {
274-
val cause = e.cause
275-
assertThat(cause).isNotNull()
276-
assertThat(cause is NumberFormatException).isTrue()
277-
}
271+
}.isInstanceOf<InvocationTargetException>()
272+
.cause()
273+
.isNotNull()
274+
.isInstanceOf<NumberFormatException>()
278275
}
279276

280277
/**
@@ -323,14 +320,12 @@ class ExampleLoadingTest {
323320
val args = arrayOf("example") // Insufficient args will cause ContextCreationException
324321

325322
// Act & Assert
326-
// TODO: Replace try-catch with assertFailure - https://github.com/bedaHovorka/interlockSim/issues/47
327-
try {
328-
val method = mainClass.getMethod("shuntingLoop", SimulationContextFactory::class.java, Array<String>::class.java)
323+
val method = mainClass.getMethod("shuntingLoop", SimulationContextFactory::class.java, Array<String>::class.java)
324+
assertFailure {
329325
method.invoke(main, factory, args)
330-
throw AssertionError("Should have thrown InvocationTargetException")
331-
} catch (e: InvocationTargetException) {
332-
assertThat(e.cause).isNotNull()
333-
}
326+
}.isInstanceOf<InvocationTargetException>()
327+
.cause()
328+
.isNotNull()
334329
}
335330

336331
/**
@@ -343,14 +338,11 @@ class ExampleLoadingTest {
343338
val mainClass = Main::class.java
344339

345340
// Act & Assert
346-
// TODO: Replace try-catch with assertFailure - https://github.com/bedaHovorka/interlockSim/issues/48
347-
try {
341+
assertFailure {
348342
mainClass.getMethod("invalidExample", SimulationContextFactory::class.java, Array<String>::class.java)
349-
throw AssertionError("Should have thrown NoSuchMethodException")
350-
} catch (e: NoSuchMethodException) {
351-
assertThat(e.message).isNotNull()
352-
assertThat((e.message ?: "").contains("invalidExample")).isTrue()
353-
}
343+
}.isInstanceOf<NoSuchMethodException>()
344+
.transform { it.message ?: "" }
345+
.contains("invalidExample")
354346
}
355347

356348
/**

0 commit comments

Comments
 (0)