micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.09k stars 1.07k forks source link

Micronaut is unable to inject dependencies into nested tests when using KSP #10941

Open cainecridland opened 4 months ago

cainecridland commented 4 months ago

Expected Behavior

If a @MicronautTest test and its injected dependencies are within a nested class, the dependencies are injected when the test in the nested class runs.

Actual Behaviour

The dependencies are not injected, leading to the following error: kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized

Steps To Reproduce

Reproduction steps are available in the README of the example application below, but generally:

  1. Create a test class that uses the @MicronautTest annotation
  2. Create a nested inner class (with the @Nested annotation) containing dependencies injected using the @Inject annotation
  3. Create a test within the inner class that uses the injected dependencies.
  4. The test will fail, but will pass if you move everything outside of the nested inner class.

Environment Information

Example Application

https://github.com/cainecridland/micronaut-nested-test-injection-problem-example

Version

4.4.0

nilols commented 3 months ago

A workaround seems to be constructor-based injection

image