From efd123cb1e69b990a8010b62762bfaad9c543481 Mon Sep 17 00:00:00 2001 From: Xavier Gouchet Date: Wed, 4 Dec 2019 10:45:37 +0100 Subject: [PATCH] :bug: (JUnit5): Fix the error message when a test fails --- CHANGELOG.md | 4 ++++ .../fr/xgouchet/elmyr/ForgeConfigurator.kt | 7 +++++++ .../elmyr/junit5/ForgeConfiguration.kt | 2 +- .../xgouchet/elmyr/junit5/ForgeExtension.kt | 19 +++++++++++++++---- .../elmyr/junit5/ForgeExtensionTest.kt | 11 ++++++----- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cbd594..eba8c78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ #### `core` - Add the nullable value forgeries + +#### `junit5` + + - Fix the error message when a test fails #### `jvm` diff --git a/core/src/main/kotlin/fr/xgouchet/elmyr/ForgeConfigurator.kt b/core/src/main/kotlin/fr/xgouchet/elmyr/ForgeConfigurator.kt index b7ad73e..b6247a5 100644 --- a/core/src/main/kotlin/fr/xgouchet/elmyr/ForgeConfigurator.kt +++ b/core/src/main/kotlin/fr/xgouchet/elmyr/ForgeConfigurator.kt @@ -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) {} + } } diff --git a/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeConfiguration.kt b/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeConfiguration.kt index 677c694..f215db2 100644 --- a/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeConfiguration.kt +++ b/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeConfiguration.kt @@ -14,6 +14,6 @@ import org.junit.platform.commons.annotation.Testable @MustBeDocumented @Inherited annotation class ForgeConfiguration( - val value: KClass, + val value: KClass = ForgeConfigurator.NoOp::class, val seed: Long = 0L ) diff --git a/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtension.kt b/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtension.kt index e88555d..104e9d8 100644 --- a/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtension.kt +++ b/junit5/src/main/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtension.kt @@ -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, @@ -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 diff --git a/junit5/src/test/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtensionTest.kt b/junit5/src/test/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtensionTest.kt index 7fd0225..5926628 100644 --- a/junit5/src/test/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtensionTest.kt +++ b/junit5/src/test/kotlin/fr/xgouchet/elmyr/junit5/ForgeExtensionTest.kt @@ -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 @@ -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