Skip to content
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

Support @CucumberContextConfiguration as a meta-annotation #2491

Closed
drekbour opened this issue Mar 2, 2022 · 2 comments · Fixed by #2630
Closed

Support @CucumberContextConfiguration as a meta-annotation #2491

drekbour opened this issue Mar 2, 2022 · 2 comments · Fixed by #2630
Labels
good first issue Good for newcomers 🙏 help wanted Help wanted - not prioritized by core team ⚡ enhancement Request for new functionality

Comments

@drekbour
Copy link

drekbour commented Mar 2, 2022

🤔 What's the problem you're trying to solve?

Support developers creating meta-annotations to wrap up common Spring testing stuff to also include @CucumberContextConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited

@CucumberContextConfiguration // Required for Cucumber + Spring
@EnableConfigurationProperties // Required for Cucumber + Spring Boot
@ComponentScan("some.test.stuff") 
@ContextConfiguration(classes = SomeGeneralPurposeTestContext.class)
@TestPropertySource(properties = "testprop=value_for_testing")
@ActiveProfiles("test")
public @interface MyTestAnnotation {}
@SpringBootTest
@MyTestAnnotation
public class TestSteps {
...

All of the above works except for @CucumberContextConfiguration

Additional context

Currently causes this as cucumber-spring tries to directly instantiate the annotated class before considering it may be a meta-annotation.

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [my.package.MyTestAnnotation]: Specified class is an interface
@mpkorstanje mpkorstanje added ⚡ enhancement Request for new functionality 🙏 help wanted Help wanted - not prioritized by core team good first issue Good for newcomers labels Mar 4, 2022
@mpkorstanje
Copy link
Contributor

mpkorstanje commented Apr 10, 2022

It should be possible to use AnnotatedElementUtils to determine if a class is meta-annotated with CucumberContextConfiguration or inherits the annotation somehow.

This is currently checked in both the SpringFactory and SpringBackend, this should become a single method:

private static boolean hasComponentAnnotation(Annotation annotation) {
return hasAnnotation(annotation, Collections.singleton(Component.class));
}

.filter((Class clazz) -> clazz.getAnnotation(CucumberContextConfiguration.class) != null)

Additionally the SpringBackend should check that the annotated element is a class and a non-abstract to prevent problems such as #2515.

@mpkorstanje mpkorstanje changed the title cucumber-spring @CucumberContextConfiguration as a meta-annotation Support @CucumberContextConfiguration as a meta-annotation Apr 10, 2022
m-schlatt pushed a commit to m-schlatt/cucumber-jvm that referenced this issue Oct 24, 2022
…tion

 Using @CucumberContextConfiguration as a meta-annotation caused a CucumberBackendException because SpringFactory only realized raw use of the class. Method hasCucumberContextConfiguration does now recognize the use of @CucumberContextConfiguration as meta-annotation or with inheritance.

 Also SpringBackend#loadGlue filters out abstract classes and interfaces to not try to instantiate what cannot be instantiated.
@m-schlatt
Copy link
Contributor

Hi, I tried to implement the suggested changes. Hope I got it right. But this is like my first commit, so any feedback is appreciated.

m-schlatt pushed a commit to m-schlatt/cucumber-jvm that referenced this issue Oct 30, 2022
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers 🙏 help wanted Help wanted - not prioritized by core team ⚡ enhancement Request for new functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants