From 7dfad5086c76a57e371d828b59b2bfdc4434c5c2 Mon Sep 17 00:00:00 2001 From: Christoph Deppisch Date: Wed, 31 Jan 2024 16:58:46 +0100 Subject: [PATCH] fix(#1116): Fix CitrusLifecycleHooks to not break Cucumber API - Do not use Cucumber before/after annotations as part of CitrusLifecycleHooks as this may lead to errors when loading the class with normal glue code API - CitrusLifecycleHooks are only supposed to be loaded by the special Citrus backend implementation --- .../cucumber/CitrusLifecycleHooks.java | 4 --- .../cucumber/backend/CitrusBackend.java | 26 +++++-------------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/CitrusLifecycleHooks.java b/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/CitrusLifecycleHooks.java index b42e69f4a1..64c7d7979c 100644 --- a/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/CitrusLifecycleHooks.java +++ b/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/CitrusLifecycleHooks.java @@ -25,8 +25,6 @@ import org.citrusframework.context.TestContext; import org.citrusframework.cucumber.backend.Scenario; import org.citrusframework.exceptions.CitrusRuntimeException; -import io.cucumber.java.After; -import io.cucumber.java.Before; /** * @author Christoph Deppisch @@ -43,7 +41,6 @@ public class CitrusLifecycleHooks { @CitrusResource private TestContext context; - @Before public void before(Scenario scenario) { if (runner != null) { runner.name(scenario.getName()); @@ -52,7 +49,6 @@ public void before(Scenario scenario) { } } - @After public void after(Scenario scenario) { if (runner != null) { if (context != null && scenario.isFailed()) { diff --git a/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/backend/CitrusBackend.java b/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/backend/CitrusBackend.java index e836d46405..012bfd43e5 100644 --- a/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/backend/CitrusBackend.java +++ b/runtime/citrus-cucumber/src/main/java/org/citrusframework/cucumber/backend/CitrusBackend.java @@ -20,18 +20,15 @@ import java.net.URI; import java.util.List; -import org.citrusframework.CitrusInstanceManager; -import org.citrusframework.cucumber.CitrusLifecycleHooks; -import org.citrusframework.cucumber.CitrusReporter; import io.cucumber.core.backend.Backend; import io.cucumber.core.backend.Container; import io.cucumber.core.backend.Glue; import io.cucumber.core.backend.Lookup; import io.cucumber.core.backend.Snippet; import io.cucumber.core.exception.CucumberException; -import io.cucumber.core.resource.ClasspathSupport; -import io.cucumber.java.After; -import io.cucumber.java.Before; +import org.citrusframework.CitrusInstanceManager; +import org.citrusframework.cucumber.CitrusLifecycleHooks; +import org.citrusframework.cucumber.CitrusReporter; /** * @author Christoph Deppisch @@ -58,29 +55,18 @@ public CitrusBackend(Lookup lookup, Container container) { @Override public void loadGlue(Glue glue, List gluePaths) { try { - if (!gluePaths.contains(getLifecycleHooksGluePath()) && container.addClass(CitrusLifecycleHooks.class)) { + if (container.addClass(CitrusLifecycleHooks.class)) { Method beforeMethod = CitrusLifecycleHooks.class.getMethod("before", Scenario.class); - Before beforeAnnotation = beforeMethod.getAnnotation(Before.class); - glue.addBeforeHook(new CitrusHookDefinition(beforeMethod, beforeAnnotation.value(), beforeAnnotation.order(), lookup)); + glue.addBeforeHook(new CitrusHookDefinition(beforeMethod, "", 10000, lookup)); Method afterMethod = CitrusLifecycleHooks.class.getMethod("after", Scenario.class); - After afterAnnotation = afterMethod.getAnnotation(After.class); - glue.addAfterHook(new CitrusHookDefinition(afterMethod, afterAnnotation.value(), afterAnnotation.order(), lookup)); + glue.addAfterHook(new CitrusHookDefinition(afterMethod, "", 10000, lookup)); } } catch (NoSuchMethodException e) { throw new CucumberException("Unable to add Citrus lifecycle hooks"); } } - /** - * Helper to create proper URI pointing to {@link CitrusLifecycleHooks}. - * @return - */ - private static URI getLifecycleHooksGluePath() { - return URI.create(ClasspathSupport.CLASSPATH_SCHEME_PREFIX + - ClasspathSupport.resourceNameOfPackageName(CitrusLifecycleHooks.class.getPackage().getName())); - } - @Override public void buildWorld() { }