Skip to content

Commit

Permalink
fix(#1116): Fix CitrusLifecycleHooks to not break Cucumber API
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
christophd committed Jan 31, 2024
1 parent 3b2126f commit 7dfad50
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,7 +41,6 @@ public class CitrusLifecycleHooks {
@CitrusResource
private TestContext context;

@Before
public void before(Scenario scenario) {
if (runner != null) {
runner.name(scenario.getName());
Expand All @@ -52,7 +49,6 @@ public void before(Scenario scenario) {
}
}

@After
public void after(Scenario scenario) {
if (runner != null) {
if (context != null && scenario.isFailed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,29 +55,18 @@ public CitrusBackend(Lookup lookup, Container container) {
@Override
public void loadGlue(Glue glue, List<URI> 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() {
}
Expand Down

0 comments on commit 7dfad50

Please sign in to comment.