Skip to content

Commit 9203085

Browse files
mpkorstanjemlvandijk
authored andcommitted
Fix Java8StepDefinition.isDefinedAt
Closes #1254
1 parent e57cd9e commit 9203085

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

java8/src/main/java/cucumber/runtime/java8/Java8StepDefinition.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public <T extends StepdefBody> Java8StepDefinition(String pattern, long timeoutM
3838
this.body = body;
3939

4040
this.argumentMatcher = new JdkPatternArgumentMatcher(this.pattern);
41-
this.location = new Exception().getStackTrace()[3];
41+
this.location = new Exception().getStackTrace()[2];
4242
this.method = getAcceptMethod(body.getClass());
4343
try {
4444
Class<?>[] arguments = resolveRawArguments(bodyClass, body.getClass());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cucumber.runtime.java8;
2+
3+
import cucumber.runtime.HookDefinition;
4+
import cucumber.runtime.StepDefinition;
5+
import cucumber.runtime.java.LambdaGlueRegistry;
6+
import org.hamcrest.CustomTypeSafeMatcher;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.ExpectedException;
10+
11+
public class Java8LambdaStepDefinitionMarksCorrectStackElementTest {
12+
13+
@Rule
14+
public ExpectedException expectedException = ExpectedException.none();
15+
16+
private final MyLambdaGlueRegistry myLambdaGlueRegistry = new MyLambdaGlueRegistry();
17+
18+
@Test
19+
public void exception_from_step_should_be_defined_at_step_definition_class() throws Throwable {
20+
LambdaGlueRegistry.INSTANCE.set(myLambdaGlueRegistry);
21+
new SomeLambdaStepDefs();
22+
final StepDefinition stepDefinition = myLambdaGlueRegistry.getStepDefinition();
23+
24+
expectedException.expect(new CustomTypeSafeMatcher<Throwable>("exception with matching stack trace") {
25+
@Override
26+
protected boolean matchesSafely(Throwable item) {
27+
for (StackTraceElement stackTraceElement : item.getStackTrace()) {
28+
if(stepDefinition.isDefinedAt(stackTraceElement)){
29+
return SomeLambdaStepDefs.class.getName().equals(stackTraceElement.getClassName());
30+
}
31+
32+
}
33+
return false;
34+
}
35+
});
36+
37+
stepDefinition.execute("en", new Object[0]);
38+
}
39+
40+
41+
private class MyLambdaGlueRegistry implements LambdaGlueRegistry {
42+
43+
private StepDefinition stepDefinition;
44+
45+
@Override
46+
public void addStepDefinition(StepDefinition stepDefinition) {
47+
this.stepDefinition = stepDefinition;
48+
}
49+
50+
@Override
51+
public void addBeforeHookDefinition(HookDefinition beforeHook) {
52+
53+
}
54+
55+
@Override
56+
public void addAfterHookDefinition(HookDefinition afterHook) {
57+
58+
}
59+
60+
public StepDefinition getStepDefinition() {
61+
return stepDefinition;
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cucumber.runtime.java8;
2+
3+
import cucumber.api.java8.En;
4+
5+
final class SomeLambdaStepDefs implements En {
6+
7+
SomeLambdaStepDefs() {
8+
Given("I have a some step definition", () -> {
9+
throw new Exception();
10+
});
11+
}
12+
13+
}

0 commit comments

Comments
 (0)