weld / weld-testing

Set of test framework extensions (JUnit 4, JUnit 5, Spock) to enhance the testing of CDI components via Weld. Supports Weld 5.
http://weld.cdi-spec.org/
Apache License 2.0
102 stars 30 forks source link

Unsatisfied dependencies using Decorator and WeldJunit5AutoExtension #171

Closed beatngu13 closed 9 months ago

beatngu13 commented 9 months ago

I'm trying out the CDI decorator pattern, here's the corresponding commit:

https://github.com/beatngu13/playground/commit/7873516adddd2402a45c14aab57ce97b327c4141

Unfortunately, the (currently @Disabled) test fails with:

INFO: WELD-000411: Observer method [BackedAnnotatedMethod] org.jboss.weld.junit5.auto.TestInstanceInjectionExtension.rewriteTestClassScope(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type MyService with qualifiers @Default
  at injection point [UnbackedAnnotatedField] @Inject com.github.beatngu13.playground.cdi.MyServiceTest.myService
  at com.github.beatngu13.playground.cdi.MyServiceTest.myService(MyServiceTest.java:0)

I based my setup on this example and wonder what am I doing wrong?

manovotn commented 9 months ago

Hello @beatngu13,

your code makes sense except you are missing a bean defining annotation on the implementing bean (MyServiceImpl). Just add @Dependent (or other scope of your preference) on it and it will work.

The reason you need this is because default discovery mode (since CDI 4.0) is annotated; by using @AddPackages(...) you are telling Weld to scan those packages for beans but it will still only discover those that have bean defining annotations.

I am going to close this issue but if you think your question isn't answered, feel free to reopen or just keep the discussion going.

beatngu13 commented 9 months ago

@manovotn this indeed works – thanks a lot for the prompt answer and great explanation! 🙏