Skip to content

Commit

Permalink
fix: corrected duplicate step when using interface for #2757
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Kronegg committed May 19, 2023
1 parent b53204e commit d31a8bc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [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)

### Added
- [Java] Added support for step definition classes with interfaces ([#2757](https://github.com/cucumber/cucumber-jvm/issues/2757) Julien Kronegg)

## [7.12.0] - 2023-04-29
### Added
- [JUnit Platform Engine] Add constant for fixed.max-pool-size property ([#2713](https://github.com/cucumber/cucumber-jvm/pull/2713) M.P. Korstanje)
Expand Down
7 changes: 5 additions & 2 deletions cucumber-java/src/main/java/io/cucumber/java/JavaBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.cucumber.core.resource.ClasspathScanner;
import io.cucumber.core.resource.ClasspathSupport;

import java.lang.reflect.Modifier;
import java.net.URI;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -39,8 +40,10 @@ public void loadGlue(Glue glue, List<URI> gluePaths) {
.flatMap(Collection::stream)
.distinct()
.forEach(aGlueClass -> scan(aGlueClass, (method, annotation) -> {
container.addClass(method.getDeclaringClass());
glueAdaptor.addDefinition(method, annotation);
if (!Modifier.isVolatile(method.getModifiers())) {
container.addClass(method.getDeclaringClass());
glueAdaptor.addDefinition(method, annotation);
}
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.cucumber.core.backend.ObjectFactory;
import io.cucumber.core.backend.StepDefinition;
import io.cucumber.java.steps.Steps;
import io.cucumber.java.stepswithinterface.StepsWithInterface;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -14,7 +15,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 Expand Up @@ -54,6 +54,13 @@ void finds_step_definitions_by_classpath_url() {
verify(factory).addClass(Steps.class);
}

@Test
void finds_step_definitions_by_classpath_url_with_interface() {
backend.loadGlue(glue, singletonList(URI.create("classpath:io/cucumber/java/stepswithinterface")));
backend.buildWorld();
verify(factory).addClass(StepsWithInterface.class);
}

@Test
void finds_step_definitions_once_by_classpath_url() {
backend.loadGlue(glue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cucumber.java.stepswithinterface;

public interface StepsInterface {

Number test();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.cucumber.java.stepswithinterface;

import io.cucumber.java.en.Given;

/**
* Having a class which implements a parent class will lead to duplicate the
* methods which return specialized type (here Number for the interface and
* Integer for the subclass). The original method has a "volatile" modifier.
*/
public class StepsWithInterface implements StepsInterface {

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

}

0 comments on commit d31a8bc

Please sign in to comment.