Skip to content

Commit

Permalink
[Java] Fix duplicate step defintion when using interfaces (#2759)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Kronegg <julien [at] kronegg.ch>
Co-authored-by: M.P. Korstanje <[email protected]>
  • Loading branch information
jkronegg and mpkorstanje authored May 26, 2023
1 parent b53204e commit 383ab6d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- [Core] Fixed `cucumber.publish.enabled=false` ([#2747](https://github.com/cucumber/cucumber-jvm/pull/2747) M.P. Korstanje)
- [JUnit Platform Engine] Fixed `cucumber.publish.enabled=false` ([#2747](https://github.com/cucumber/cucumber-jvm/pull/2747) M.P. Korstanje)
- [Java] Fixed duplicate step definition for classes with interfaces ([#2757](https://github.com/cucumber/cucumber-jvm/issues/2757) Julien Kronegg)

## [7.12.0] - 2023-04-29
### Added
Expand Down
11 changes: 11 additions & 0 deletions cucumber-java/src/main/java/io/cucumber/java/MethodScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ private static void scan(BiConsumer<Method, Annotation> consumer, Class<?> aClas
if (Object.class.equals(method.getDeclaringClass())) {
return;
}

// exclude bridge methods: when a class implements a method
// from the interface but specializes the return type, two methods will
// be generated. One with the return type of the interface and one
// with the specialized return type. The former is a bridge method.
// Depending on the JVM, the method annotations are also applied to
// the bridge method.
if (method.isBridge()) {
return;
}

scan(consumer, aClass, method, method.getAnnotations());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URI;
import java.util.Arrays;
import java.util.List;

import static java.lang.Thread.currentThread;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.cucumber.java;

import io.cucumber.java.en.Given;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -41,6 +42,13 @@ void scan_ignores_object() {
assertThat(scanResult, empty());
}

@Test
void scan_ignores_bridge_methods() throws NoSuchMethodException {
Method method = SpecializedReturnType.class.getMethod("test");
MethodScanner.scan(SpecializedReturnType.class, backend);
assertThat(scanResult, contains(new SimpleEntry<>(method, method.getAnnotations()[0])));
}

@Test
void scan_ignores_non_instantiable_class() {
MethodScanner.scan(NonStaticInnerClass.class, backend);
Expand Down Expand Up @@ -81,4 +89,17 @@ public void m() {

}

public interface GenericReturnType {
Number test();

}

public static class SpecializedReturnType implements GenericReturnType {

@Given("test")
public Integer test() {
return 1;
}

}
}

0 comments on commit 383ab6d

Please sign in to comment.