Skip to content

Commit

Permalink
🐛 (JUnit5): Fix the error message when a test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Dec 4, 2019
1 parent 6db9a4a commit efd123c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#### `core`

- Add the nullable value forgeries

#### `junit5`

- Fix the error message when a test fails

#### `jvm`

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/kotlin/fr/xgouchet/elmyr/ForgeConfigurator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ interface ForgeConfigurator {
* Allows you to configure a [Forge] instance.
*/
fun configure(forge: Forge)

/**
* A no-op implementation of [ForgeConfigurator].
*/
object NoOp : ForgeConfigurator {
override fun configure(forge: Forge) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import org.junit.platform.commons.annotation.Testable
@MustBeDocumented
@Inherited
annotation class ForgeConfiguration(
val value: KClass<out ForgeConfigurator>,
val value: KClass<out ForgeConfigurator> = ForgeConfigurator.NoOp::class,
val seed: Long = 0L
)
19 changes: 15 additions & 4 deletions junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ class ForgeExtension :

/** @inheritdoc */
override fun handleTestExecutionException(context: ExtensionContext, throwable: Throwable) {
val message = "<%s.%s()> failed with Forge seed 0x%xL\n" +
"Add the following line in your @BeforeEach method to reproduce :\n\n" +
"\tforge.setSeed(0x%xL);\n"
val configuration = getConfigurations(context).firstOrNull()
val message = if (configuration == null) {
"<%s.%s()> failed with Forge seed 0x%xL\n" +
"Add the following @ForgeConfiguration annotation to your test class :\n\n" +
"\t@ForgeConfiguration(seed = 0x%xL)\n"
} else {
"<%s.%s()> failed with Forge seed 0x%xL\n" +
"Add the seed in your @ForgeConfiguration annotation :\n\n" +
"\t@ForgeConfiguration(value = ${configuration.value.simpleName}::class, seed = 0x%xL)\n"
}
System.err.println(
message.format(
Locale.US,
Expand Down Expand Up @@ -163,7 +170,11 @@ class ForgeExtension :
result.add(annotation.get())
}

currentContext = currentContext.parent.get()
if (currentContext.parent.isPresent) {
currentContext = currentContext.parent.get()
} else {
break
}
}

return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,11 @@ class ForgeExtensionTest {
.isEqualTo(
"<${mockTarget.javaClass.simpleName}.${fakeMethod.name}()> failed " +
"with Forge seed 0x${forge.seed.toString(16)}L\n" +
"Add the following line in your @BeforeEach method to reproduce :\n" +
"Add the following @ForgeConfiguration annotation to your test class :\n" +
"\n" +
"\tforge.setSeed(0x${forge.seed.toString(16)}L);\n" +
"\n")
"\t@ForgeConfiguration(seed = 0x${forge.seed.toString(16)}L)\n" +
"\n"
)
}

// endregion
Expand Down Expand Up @@ -417,10 +418,10 @@ class Reflekta(@Forgery s: String) {
fun withNotBool(@BoolForgery s: String) {
}

fun withBoolInvalid1(@BoolForgery(probability = -1f)b: Boolean) {
fun withBoolInvalid1(@BoolForgery(probability = -1f) b: Boolean) {
}

fun withBoolInvalid2(@BoolForgery(probability = 2f)b: Boolean) {
fun withBoolInvalid2(@BoolForgery(probability = 2f) b: Boolean) {
}

// endregion
Expand Down

0 comments on commit efd123c

Please sign in to comment.