Skip to content

Commit

Permalink
cucumber#2491 Support @CucumberContextConfiguration as a meta-annota…
Browse files Browse the repository at this point in the history
…tion

Adds suggestions from review: renames test package, more focused testing and updates the changelog.

Method hasCucumberContextConfiguration now uses AnnotatedElementUtils isAnnotated with considers all merged annotations (including inherited and meta-annotation).
  • Loading branch information
michael committed Oct 30, 2022
1 parent c63783b commit 6461925
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 44 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- [Spring] Support @CucumberContextConfiguration as a meta-annotation ([2491](https://github.com/cucumber/cucumber-jvm/issues/2491) Michael Schlatt)

### Changed
- [Core] Update dependency io.cucumber:gherkin to v24.1
- [Core] Delegate encoding and BOM handling to gherkin ([2624](https://github.com/cucumber/cucumber-jvm/issues/2624) M.P. Korstanje)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ private static void checkNoComponentAnnotations(Class<?> type) {
}
}

public static boolean hasCucumberContextConfiguration(Class<?> stepClass) {
return stepClass.getAnnotation(CucumberContextConfiguration.class) != null
|| AnnotatedElementUtils.hasMetaAnnotationTypes(stepClass, CucumberContextConfiguration.class)
|| AnnotatedElementUtils.getMergedAnnotation(stepClass, CucumberContextConfiguration.class) != null;
static boolean hasCucumberContextConfiguration(Class<?> stepClass) {
return AnnotatedElementUtils.isAnnotated(stepClass, CucumberContextConfiguration.class);
}

private void checkOnlyOneClassHasCucumberContextConfiguration(Class<?> stepClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import io.cucumber.core.backend.Glue;
import io.cucumber.core.backend.ObjectFactory;
import io.cucumber.spring.annotationconfig.AnnotationContextConfiguration;
import io.cucumber.spring.cucontextconfig.AbstractWithComponentAnnotation;
import io.cucumber.spring.cucontextconfig.WithMetaAnnotation;
import io.cucumber.spring.cucumbercontextconfigannotation.AbstractWithComponentAnnotation;
import io.cucumber.spring.cucumbercontextconfigannotation.AnnotatedInterface;
import io.cucumber.spring.cucumbercontextconfigannotation.WithMetaAnnotation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -54,9 +55,24 @@ void finds_annotaiton_context_configuration_once_by_classpath_url() {
@Test
void ignoresAbstractClassWithCucumberContextConfiguration() {
backend.loadGlue(glue, singletonList(
URI.create("classpath:io/cucumber/spring/cucontextconfig")));
URI.create("classpath:io/cucumber/spring/cucumbercontextconfigannotation")));
backend.buildWorld();
verify(factory, times(0)).addClass(AbstractWithComponentAnnotation.class);
}

@Test
void ignoresInterfaceWithCucumberContextConfiguration() {
backend.loadGlue(glue, singletonList(
URI.create("classpath:io/cucumber/spring/cucumbercontextconfigannotation")));
backend.buildWorld();
verify(factory, times(0)).addClass(AnnotatedInterface.class);
}

@Test
void considersClassWithCucumberContextConfigurationMetaAnnotation() {
backend.loadGlue(glue, singletonList(
URI.create("classpath:io/cucumber/spring/cucumbercontextconfigannotation")));
backend.buildWorld();
verify(factory, times(1)).addClass(WithMetaAnnotation.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import io.cucumber.spring.componentannotation.WithControllerAnnotation;
import io.cucumber.spring.contextconfig.BellyStepDefinitions;
import io.cucumber.spring.contexthierarchyconfig.WithContextHierarchyAnnotation;
import io.cucumber.spring.cucontextconfig.WithMetaAnnotation;
import io.cucumber.spring.cucumbercontextconfigannotation.WithInheritedAnnotation;
import io.cucumber.spring.cucumbercontextconfigannotation.WithMetaAnnotation;
import io.cucumber.spring.dirtiescontextconfig.DirtiesContextBellyStepDefinitions;
import io.cucumber.spring.metaconfig.dirties.DirtiesContextBellyMetaStepDefinitions;
import io.cucumber.spring.metaconfig.general.BellyMetaStepDefinitions;
Expand Down Expand Up @@ -360,13 +361,18 @@ void shouldBeStoppableWhenFacedWithFailedApplicationContext(Class<?> contextConf
assertDoesNotThrow(factory::stop);
}

@ParameterizedTest
@ValueSource(classes = {
WithMetaAnnotation.class,
})
void shouldNotFailWithCucumberContextConfigurationMetaAnnotation(Class<?> contextConfiguration) {
@Test
void shouldNotFailWithCucumberContextConfigurationMetaAnnotation() {
final ObjectFactory factory = new SpringFactory();
factory.addClass(contextConfiguration);
factory.addClass(WithMetaAnnotation.class);

assertDoesNotThrow(factory::start);
}

@Test
void shouldNotFailWithCucumberContextConfigurationInheritedAnnotation() {
final ObjectFactory factory = new SpringFactory();
factory.addClass(WithInheritedAnnotation.class);

assertDoesNotThrow(factory::start);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.cucumber.spring.cucontextconfig;
package io.cucumber.spring.cucumbercontextconfigannotation;

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.test.context.ContextConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cucumber.spring.cucumbercontextconfigannotation;

import io.cucumber.spring.CucumberContextConfiguration;

@CucumberContextConfiguration
public interface AnnotatedInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.cucumber.spring.cucumbercontextconfigannotation;

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.test.context.ContextConfiguration;

import java.lang.annotation.*;

public class WithInheritedAnnotation extends ParentClass {
}

@InheritableCumberContextConfiguration
class ParentClass {
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@CucumberContextConfiguration
@ContextConfiguration("classpath:cucumber.xml")
@Inherited
@interface InheritableCumberContextConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.cucumber.spring.cucumbercontextconfigannotation;

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.test.context.ContextConfiguration;

import java.lang.annotation.*;

@MyTestAnnotation
public class WithMetaAnnotation {
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@CucumberContextConfiguration
@ContextConfiguration("classpath:cucumber.xml")
@interface MyTestAnnotation {
}

0 comments on commit 6461925

Please sign in to comment.