Marc von Renteln Marc von Renteln - 10 months ago 81
Java Question

Cucumber CDI injection does not work with beans in src/main/java

I have a JEE application in a maven project using JUnit and Cucumber to test my code. The application is using CDI with weld-se in test scope.

I have created a simple feature file


Scenario: Identified user
Given I am user A

Then I created the corresponding cucumber step:

public class FirstSteps {

UserService userService;

@Given("^I am user A$")
public void i_am_user_A() throws Throwable {
User user ="user A").create();


User Service is an Interface with one trivial implementation:

public interface UserService {
User getActualUser();

public class FakeUserService implements UserService {

public User getActualUser() {
return"user A").create();


There are empty beans.xml files in

The step is located in
, the service interface and its implementation in

And here is the JUnit-Test for it:

features = {"classpath:specifikation"},
glue = {"mypackage"})
public final class CucumberTests {

The step is found by cucumber and gets executed. But CDI does not work:

Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UserService with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject mypackage.FirstSteps.userService

I have found this issue on github for cucumber. But the solution does not work for me. I already have two empty beans.xml files in

It seems like the weld container creates a context for
only and everything in
does not belong to it. When I move the service interface and implementation to
it works...

Is there a way to reference beans in

EDIT: corrected location of the service interface


Assuming that your step is located in src/test/java and your service (interface) is located in src/main/java and not in src/(test|main)/resources (typo?), your setup should work.

Maybe check the following:

  1. Ensure that your src/main/java and src/test/java folders are on the same classpath.
  2. Assuming that you are using a current version of Weld which is CDI >1.1 compliant, ensure that both folders are explicit bean archives.
  3. Ensure your beans.xml files are literally empty or have bean-discovery-mode set to all.

If that does not work either, try to annotate your FakeUserService.