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

BeanInstantiationException with cucumber 7 and kotlin (lambda) #2413

Closed
paul58914080 opened this issue Nov 3, 2021 · 2 comments
Closed

BeanInstantiationException with cucumber 7 and kotlin (lambda) #2413

paul58914080 opened this issue Nov 3, 2021 · 2 comments

Comments

@paul58914080
Copy link

Describe the bug
There seems to be an issue which was working earlier when I tried to adapt to the new release of cucumber 7 with spring boot test and kotlin using lambda's. I get the following exception

io.cucumber.core.backend.CucumberBackendException: Error creating bean with name 'io.cucumber.kotlin.SampleStepDef': Bean instantiation via constructor failed

More logs

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.cucumber.kotlin.SampleStepDef]: Constructor threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:224)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:311)
	... 103 more
Caused by: java.lang.NullPointerException
	at io.cucumber.java8.En.Given(En.java:374)
	at io.cucumber.kotlin.SampleStepDef.<init>(SampleStepDef.kt:18)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at kotlin.reflect.jvm.internal.calls.CallerImpl$Constructor.call(CallerImpl.kt:41)
	at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:108)
	at kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod$kotlin_reflection(KCallableImpl.kt:159)
	at kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:112)
	at org.springframework.beans.BeanUtils$KotlinDelegate.instantiateClass(BeanUtils.java:867)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:196)
	... 105 more

To Reproduce
Steps to reproduce the behavior:

  1. Clone https://github.com/paul58914080/sample-cucumber
  2. Run mvn test

Expected behavior
This was working fine earlier in <v7. The tests were being executed and the same can be observed in devs-from-matrix/hexagonal-spring-boot-kotlin#238 where the cucumber tests are executed fine with v6.11.0

Your Environment

  • Versions used 7.0.0
  • Operating System and version : MacOS 11.5.1
  • Build tool : maven
@paul58914080 paul58914080 changed the title Bean initialisation failing with cucumber 7 and kotlin (lambda) BeanInstantiationException with cucumber 7 and kotlin (lambda) Nov 3, 2021
@mpkorstanje
Copy link
Contributor

The NPE isn't very pretty but since v7.0.0 you can't use lambda step definitions in a class annotated with @CucumberContextConfiguration.

Since v7 Cucumber invokes TestContextManager.{before,after}TestMethod(instance, method). This ensures that all spring test execution listeners are invoked. However this requires an instance of the class annotated with @CucumberContextConfiguration. If this class also defines lambda step definitions then these will also be instantiated. This will fail because the spring application context which we are currently in the process of starting has to be running before any steps can be registered.

In light of #2279 I don't think this will be fixed. I would suggest using either cucumber-java or separating the context configuration from step definitions.

PS: Thanks for the reproducer. Btw. You don't need @ExtendWith(SpringExtension::class), @SpringBootTest already has that as a meta annotation.

@paul58914080
Copy link
Author

paul58914080 commented Nov 4, 2021

I can work with cucumber-java until cucumber-lambda is out.

paul58914080 added a commit to devs-from-matrix/hexagonal-spring-boot-kotlin that referenced this issue Nov 5, 2021
The CucumberContextConfiguration does not work well with lambda's (cucumber-java8) hence I had to extract the configuration out of the stepdef as suggested in cucumber/cucumber-jvm#2413

Signed-off-by: Paul Williams <paul58914080@gmail.com>
paul58914080 added a commit to devs-from-matrix/hexagonal-spring-boot-java that referenced this issue Nov 5, 2021
The CucumberContextConfiguration does not work well with lambda's (cucumber-java8) hence I had to extract the configuration out of the stepdef as suggested in cucumber/cucumber-jvm#2413

Signed-off-by: Paul Williams <paul58914080@gmail.com>
paul58914080 added a commit to devs-from-matrix/hexagonal-spring-boot-java that referenced this issue Nov 6, 2021
The CucumberContextConfiguration does not work well with lambda's (cucumber-java8) hence I had to extract the configuration out of the stepdef as suggested in cucumber/cucumber-jvm#2413

Signed-off-by: Paul Williams <paul58914080@gmail.com>
paul58914080 added a commit to devs-from-matrix/hexagonal-spring-boot-kotlin that referenced this issue Nov 6, 2021
The CucumberContextConfiguration does not work well with lambda's (cucumber-java8) hence I had to extract the configuration out of the stepdef as suggested in cucumber/cucumber-jvm#2413

Signed-off-by: Paul Williams <paul58914080@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants