weld/weld-testing

Unsatisfied dependencies using Decorator and WeldJunit5AutoExtension

Closed this issue · 2 comments

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

beatngu13/playground@7873516

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?

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.

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