Skip to content

Remove framework elements from UndefinedStepException stacktrace #3002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- [JUnit Platform Engine, TestNG] Remove framework elements from `UndefinedStepException` stacktrace ([#3002](https://github.com/cucumber/cucumber-jvm/pull/3002) M.P. Korstanje)

## [7.22.2] - 2025-05-12
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.Location;
import io.cucumber.plugin.event.Result;
import io.cucumber.plugin.event.SnippetsSuggestedEvent;
import io.cucumber.plugin.event.Status;
import io.cucumber.plugin.event.TestCaseFinished;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -41,7 +43,7 @@ public void close() {

private void handleSnippetSuggestedEvent(SnippetsSuggestedEvent event) {
SnippetsSuggestedEvent.Suggestion s = event.getSuggestion();
suggestions.add(new Suggestion(s.getStep(), s.getSnippets()));
suggestions.add(new Suggestion(s.getStep(), s.getSnippets(), event.getUri(), event.getStepLocation()));
}

private void handleTestCaseFinished(TestCaseFinished event) {
Expand Down Expand Up @@ -80,10 +82,22 @@ public static final class Suggestion {

final String step;
final List<String> snippets;
final URI uri;
final Location location;

@Deprecated
public Suggestion(String step, List<String> snippets) {
this.step = requireNonNull(step);
this.snippets = unmodifiableList(requireNonNull(snippets));
this.uri = null;
this.location = null;
}

public Suggestion(String step, List<String> snippets, URI uri, Location location) {
this.step = requireNonNull(step);
this.snippets = unmodifiableList(requireNonNull(snippets));
this.uri = requireNonNull(uri);
this.location = requireNonNull(location);
}

public String getStep() {
Expand All @@ -94,6 +108,13 @@ public List<String> getSnippets() {
return snippets;
}

public URI getUri() {
return uri;
}

public Location getLocation() {
return location;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ final class UndefinedStepException extends IncompleteExecutionException {

UndefinedStepException(Collection<Suggestion> suggestions) {
super(createMessage(suggestions));
setStackTrace(createSyntheticStacktrace(suggestions));
}

private StackTraceElement[] createSyntheticStacktrace(Collection<Suggestion> suggestions) {
if (suggestions.isEmpty()) {
return new StackTraceElement[0];
}
Suggestion first = suggestions.iterator().next();
int line = first.getLocation().getLine();
String uri = first.getUri().toString();
String stepText = first.getStep();
StackTraceElement stackTraceElement = new StackTraceElement("✽", stepText, uri, line);
return new StackTraceElement[] { stackTraceElement };
}

private static String createMessage(Collection<Suggestion> suggestions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package io.cucumber.junit.platform.engine;

import io.cucumber.core.runtime.TestCaseResultObserver.Suggestion;
import io.cucumber.plugin.event.Location;
import org.junit.jupiter.api.Test;

import java.net.URI;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;

class UndefinedStepExceptionTest {

private final URI uri = URI.create("classpath:example.feature");
private final Location stepLocation = new Location(12, 4);

@Test
void should_generate_a_message_for_no_suggestions() {
UndefinedStepException exception = new UndefinedStepException(emptyList());
assertThat(exception.getMessage(), is("This step is undefined"));
}

@Test
void should_generate_an_empty_stacktrace_for_no_suggestions() {
UndefinedStepException exception = new UndefinedStepException(emptyList());
assertThat(exception.getStackTrace(), arrayWithSize(0));
}

@Test
void should_generate_a_message_for_one_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
singletonList(
new Suggestion("some step", singletonList("some snippet")))
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -31,11 +45,23 @@ void should_generate_a_message_for_one_suggestions() {
"some snippet\n"));
}

@Test
void should_generate_a_stacktrace_for_one_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
singletonList(
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation))

);
assertThat(exception.getStackTrace(), arrayWithSize(1));
assertThat(exception.getStackTrace()[0].toString(), equalTo("✽.some step(classpath:example.feature:12)"));
}

@Test
void should_generate_a_message_for_one_suggestions_with_multiple_snippets() {
UndefinedStepException exception = new UndefinedStepException(
singletonList(
new Suggestion("some step", asList("some snippet", "some other snippet")))
new Suggestion("some step", asList("some snippet", "some other snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -50,8 +76,9 @@ void should_generate_a_message_for_one_suggestions_with_multiple_snippets() {
void should_generate_a_message_for_two_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
asList(
new Suggestion("some step", singletonList("some snippet")),
new Suggestion("some other step", singletonList("some other snippet")))
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation),
new Suggestion("some other step", singletonList("some other snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -66,8 +93,10 @@ void should_generate_a_message_for_two_suggestions() {
void should_generate_a_message_without_duplicate_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
asList(
new Suggestion("some step", asList("some snippet", "some snippet")),
new Suggestion("some other step", asList("some other snippet", "some other snippet")))
new Suggestion("some step", asList("some snippet", "some snippet"), uri,
stepLocation),
new Suggestion("some other step", asList("some other snippet", "some other snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -82,9 +111,11 @@ void should_generate_a_message_without_duplicate_suggestions() {
void should_generate_a_message_for_three_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
asList(
new Suggestion("some step", singletonList("some snippet")),
new Suggestion("some other step", singletonList("some other snippet")),
new Suggestion("yet another step", singletonList("yet another snippet")))
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation),
new Suggestion("some other step", singletonList("some other snippet"), uri,
stepLocation),
new Suggestion("yet another step", singletonList("yet another snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ final class UndefinedStepException extends SkipException {

UndefinedStepException(Collection<Suggestion> suggestions) {
super(createMessage(suggestions));
setStackTrace(createSyntheticStacktrace(suggestions));
}

private StackTraceElement[] createSyntheticStacktrace(Collection<Suggestion> suggestions) {
if (suggestions.isEmpty()) {
return new StackTraceElement[0];
}
Suggestion first = suggestions.iterator().next();
int line = first.getLocation().getLine();
String uri = first.getUri().toString();
String stepText = first.getStep();
StackTraceElement stackTraceElement = new StackTraceElement("✽", stepText, uri, line);
return new StackTraceElement[] { stackTraceElement };
}

private static String createMessage(Collection<Suggestion> suggestions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package io.cucumber.testng;

import io.cucumber.core.runtime.TestCaseResultObserver.Suggestion;
import io.cucumber.plugin.event.Location;
import org.testng.annotations.Test;

import java.net.URI;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;

public class UndefinedStepExceptionTest {

private final URI uri = URI.create("classpath:example.feature");
private final Location stepLocation = new Location(12, 4);

@Test
public void should_generate_a_message_for_no_suggestions() {
UndefinedStepException exception = new UndefinedStepException(emptyList());
assertThat(exception.getMessage(), is("This step is undefined"));
}

@Test
void should_generate_an_empty_stacktrace_for_no_suggestions() {
UndefinedStepException exception = new UndefinedStepException(emptyList());
assertThat(exception.getStackTrace(), arrayWithSize(0));
}

@Test
public void should_generate_a_message_for_one_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
singletonList(
new Suggestion("some step", singletonList("some snippet")))
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -31,11 +45,23 @@ public void should_generate_a_message_for_one_suggestions() {
"some snippet\n"));
}

@Test
void should_generate_a_stacktrace_for_one_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
singletonList(
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation))

);
assertThat(exception.getStackTrace(), arrayWithSize(1));
assertThat(exception.getStackTrace()[0].toString(), equalTo("✽.some step(classpath:example.feature:12)"));
}

@Test
public void should_generate_a_message_for_one_suggestions_with_multiple_snippets() {
UndefinedStepException exception = new UndefinedStepException(
singletonList(
new Suggestion("some step", asList("some snippet", "some other snippet")))
new Suggestion("some step", asList("some snippet", "some other snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -50,8 +76,9 @@ public void should_generate_a_message_for_one_suggestions_with_multiple_snippets
public void should_generate_a_message_for_two_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
asList(
new Suggestion("some step", singletonList("some snippet")),
new Suggestion("some other step", singletonList("some other snippet")))
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation),
new Suggestion("some other step", singletonList("some other snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -66,8 +93,10 @@ public void should_generate_a_message_for_two_suggestions() {
public void should_generate_a_message_without_duplicate_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
asList(
new Suggestion("some step", asList("some snippet", "some snippet")),
new Suggestion("some other step", asList("some other snippet", "some other snippet")))
new Suggestion("some step", asList("some snippet", "some snippet"), uri,
stepLocation),
new Suggestion("some other step", asList("some other snippet", "some other snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand All @@ -82,9 +111,11 @@ public void should_generate_a_message_without_duplicate_suggestions() {
public void should_generate_a_message_for_three_suggestions() {
UndefinedStepException exception = new UndefinedStepException(
asList(
new Suggestion("some step", singletonList("some snippet")),
new Suggestion("some other step", singletonList("some other snippet")),
new Suggestion("yet another step", singletonList("yet another snippet")))
new Suggestion("some step", singletonList("some snippet"), uri, stepLocation),
new Suggestion("some other step", singletonList("some other snippet"), uri,
stepLocation),
new Suggestion("yet another step", singletonList("yet another snippet"), uri,
stepLocation))

);
assertThat(exception.getMessage(), is("" +
Expand Down