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

Dependency is null when injected in Step definition classes #6

Open
ahmedabbas11 opened this issue Aug 30, 2021 · 8 comments
Open

Dependency is null when injected in Step definition classes #6

ahmedabbas11 opened this issue Aug 30, 2021 · 8 comments

Comments

@ahmedabbas11
Copy link

ahmedabbas11 commented Aug 30, 2021

I have a step definition and i need to inject a dependency of a repository , but the repository is null.

@ApplicationScoped
@Unremovable
@DBUnitInterceptor
public class Steps{
    @Inject
    SomeRepository repository;
    
    @DataSet(value = "seed_my_data.yml")
    @Given("user {string} submitted a request with id {string} and service {string} and type {string}")
    public void user_submitted_a_request_with_id_and_service_and_type(String userName, String id, String service, String type) {
 
        Request request = repository.findById(id);
    }

The same repository is injected without problems in other non-cucumber tests that are marked with @QuarkusTest

Test runner class is as follows:

public class RunCucumberTest extends CucumberQuarkusTest {
}

Expected injected dependency inside the step class to be an instantiated object
Actual dependency is null

@stuartwdouglas
Copy link
Contributor

Do you have a simple reproducer I can look at?

@ahmedabbas11
Copy link
Author

While i was creating a reproducer project, i found out the issue.
The issue occurs when I run a single scenario through intellj plugin; Intellj creates a cucumber run configuration. but then dependencies inside steps are null.
But when i run from the Runner class extending CucumberQuarkusTest, tests pass without issue and dependency injection works without problem. Intellj creates a gradle run configuration at this case.

please find attached project.
https://github.com/ahmedabbas11/quarkus-cucumber-dependency

While i can runs tests from gradle now, but running all the tests each time is not practical at the long run , whe steps grow in size and it would take time to run them all, then it is necessary to be able to run single scenario.

@christophd
Copy link
Contributor

ok that makes sense to me because when running the test via Cucumber IntelliJ the @QuarkusTest annotation is not taken into account. This is simply because Cucumber is running the test and not Quarkus (via JUnit5). Not sure if the test is even run with JUnit5 when running via IntelliJ Cucumber plugin. When running the test with CucumberQuarkusTest the @QuarkusTest annotation prepares everything including @Inject support.

So at the moment I do not see a simple solution for that

@christophd
Copy link
Contributor

Ah! This PR might solve this issue: #2

@stuartwdouglas
Copy link
Contributor

Is that PR all that is required?

@tisoft
Copy link
Contributor

tisoft commented Sep 1, 2021

Following is needed:

  1. The PR Add support for running with main() method #2
  2. Adding a main method to the test class:
public static void main(String[] args) {
    runMain(CucumberOptionsTest.class, args);
}
  1. Changing the Main class in the IntelliJ Run Configuration

@smeulend
Copy link

smeulend commented Sep 3, 2021

The solution for this is restoring the original class loader within the test.

So once the context has been created, obtain the classloader and store it:
var originalClassLoader = Thread.currentThread().getContextClassLoader();

And when executing the test restore it in the test thread:
context.runTestCase(r -> {
if (originalClassLoader != null) {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
r.runPickle(p);
});

@khush2704
Copy link

@ahmedabbas11 , I want to know how you are running your cucumber tests, is it integrated with your quarkus application or you have dedicated cucumber project, using its dependency in your qaurkus application and invoking cucumber tests ?

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

6 participants