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

Beans injected in tests are removed as unused #1749

Closed
sarxos opened this issue Mar 28, 2019 · 12 comments
Closed

Beans injected in tests are removed as unused #1749

sarxos opened this issue Mar 28, 2019 · 12 comments
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Milestone

Comments

@sarxos
Copy link
Contributor

sarxos commented Mar 28, 2019

Hello @mkouba,

This is in regards to our conversation from Zulip.

Beans injected into a test are automatically removed.

I have a service:

@ApplicationScoped
public class TestService {
}

And a test:

@QuarkusTest
public class GreetingResourceTest {

    @Inject
    TestService ts;

    @Test
    void test_lookupService() {
        Assertions.assertThat(ts).isNotNull();
    }
}

Since TestService has bean defining annotation it is automatically discovered. And since it's @Inject-ed into test, it should not be autoremoved. But it's removed and test fails

The workarounds so far:

  1. Configure quarkus.arc.remove-unused-beans=false in application.preferences
  2. Add scope annotation on test, e.g. @Singleton:
@Singleton // this cause test pass
@QuarkusTest
public class GreetingResourceTest {

    @Inject
    TestService ts;

    @Test
    void test_lookupService() {
        Assertions.assertThat(ts).isNotNull();
    }
}

A failure log:

sarxos@sarxos-comp:~/workspace/getting-started$ mvn clean test
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< org.acme:getting-started >----------------------
[INFO] Building getting-started 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ getting-started ---
[INFO] Deleting /home/sarxos/workspace-2019/getting-started/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ getting-started ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ getting-started ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to /home/sarxos/workspace-2019/getting-started/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ getting-started ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/sarxos/workspace-2019/getting-started/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ getting-started ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/sarxos/workspace-2019/getting-started/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ getting-started ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.acme.quickstart.GreetingResourceTest
2019-03-28 20:37:07,486 INFO  [io.qua.dep.QuarkusAugmentor] (main) Beginning quarkus augmentation
2019-03-28 20:37:08,014 INFO  [io.qua.dep.QuarkusAugmentor] (main) Quarkus augmentation completed in 528ms
2019-03-28 20:37:08,331 INFO  [io.quarkus] (main) Quarkus 0.12.0 started in 0.300s. Listening on: http://[::]:8081
2019-03-28 20:37:08,331 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.902 s <<< FAILURE! - in org.acme.quickstart.GreetingResourceTest
[ERROR] test_lookupService  Time elapsed: 0.01 s  <<< ERROR!
org.junit.jupiter.api.extension.TestInstantiationException: TestInstanceFactory [io.quarkus.test.junit.QuarkusTestExtension] failed to instantiate test class [org.acme.quickstart.GreetingResourceTest]: Failed to inject field com.github.sarxos.abberwoult.cdi.TestService org.acme.quickstart.GreetingResourceTest.ts
Caused by: java.lang.RuntimeException: Failed to inject field com.github.sarxos.abberwoult.cdi.TestService org.acme.quickstart.GreetingResourceTest.ts
Caused by: java.lang.NullPointerException: Managed Bean [class com.github.sarxos.abberwoult.cdi.TestService] is null

2019-03-28 20:37:08,359 INFO  [io.quarkus] (main) Quarkus stopped in 0.008s
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   GreetingResourceTest.test_lookupService » TestInstantiation TestInstanceFactor...
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.112 s
[INFO] Finished at: 2019-03-28T20:37:08+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.0:test (default-test) on project getting-started: There are test failures.
[ERROR] 
[ERROR] Please refer to /home/sarxos/workspace-2019/getting-started/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
@geoand
Copy link
Contributor

geoand commented Mar 28, 2019

@mkouba Would this PR I closed perhaps be of some use?

#1545

I was also thinking that maybe we should introduce a BuildItem that could be classes that are not but we be consulted (and checked if they contain @Inject annotations) when creating the predicates that prevent beans from being removed?

@mkouba mkouba added kind/bug Something isn't working area/arc Issue related to ARC (dependency injection) labels Mar 29, 2019
@mkouba
Copy link
Contributor

mkouba commented Mar 29, 2019

@sarxos Ok, so we should clarify that this problem occurs iff an application bean (TestService) is used only in a test class (GreetingResourceTest). In other words, it's not used in the app code AND it's not used in any bean defined in src/test/java.

@geoand Yes, your proposal could one way how to address this issue. The other way would be to register @QuarkusTest as a bean defining annotation - test classes would be analyzed during test even though the test instances would not be instantiated by container.

@geoand
Copy link
Contributor

geoand commented Mar 29, 2019

@geoand Yes, your proposal could one way how to address this issue. The other way would be to register @QuarkusTest as a bean defining annotation - test classes would be analyzed during test even though the test instances would not be instantiated by container.

You are the expert, whatever you think is best :). If you don't have enough time to fix it today, I can most likely squeeze in whichever fix you prefer over the weekend.

@sarxos
Copy link
Contributor Author

sarxos commented Mar 29, 2019

@mkouba Yes, I confirm. This issue appears if and only if my CDI bean is @Inject-ed only in a test class. If I additionally inject it in application class, e.g. a resource annotated with @Path, the problem does not exist.

@sarxos
Copy link
Contributor Author

sarxos commented Mar 29, 2019

@mkouba I do not insist on fixing this, but this behaviour should be described in more details since I spent some time trying to narrow it down and I assume integrators to have similar problems when working with ArC's CDI. I can prepare PR against https://github.com/quarkusio/quarkusio.github.io/blob/develop/_guides/cdi-reference.adoc if you like. Quarkus is developed under Red Hat, am I correct? Do you require signing contributors agreement or similar like the guys from Oracle?

@mkouba
Copy link
Contributor

mkouba commented Mar 29, 2019

@sarxos I agree that we should definitely improve the docs in this area. There is already an issue: #1669.

I also think that should fix this. @geoand I lean towards the "register @QuarkusTest as a bean defining annotation" solution but your idea of having a BuildItem to specify classes that should be analyzed for injection points is also interesting. In fact, we could analyze the test classes even without a special BuildItem but the problem is that CDI resolution rules are quite complex so it wouldn't be so easy to find out wheter an injection point resolves to a particular bean.

Let's think about it over the weekend and we can decide early next week.

@sarxos Re contributors agreement - I'm not aware of any agreement. It's just an ASL2 open source project.

@geoand
Copy link
Contributor

geoand commented Mar 29, 2019

Let's think about it over the weekend and we can decide early next week.

👍

@geoand
Copy link
Contributor

geoand commented Apr 1, 2019

@mkouba I am thinking for the time being we should just go with the straight-forward of having @QuarkusTest be a bean defining annotation.
Should the need for a more complex solution (like the one I proposed above) arise, I think we could reevaluate. WDYT?

@mkouba
Copy link
Contributor

mkouba commented Apr 1, 2019

@geoand Yes, makes sense. Would you like to send a PR? BTW we should probably also check the JUnit4 runner.

@geoand
Copy link
Contributor

geoand commented Apr 1, 2019

@mkouba Sure thing, I will take care of it either tonight or tomorrow morning :)

@gsmet gsmet added this to the 0.13.0 milestone Apr 1, 2019
geoand added a commit that referenced this issue Apr 2, 2019
Allow beans to be injected in tests even if they aren't used in the application
@sarxos
Copy link
Contributor Author

sarxos commented Apr 2, 2019

Thank you @geoand :)

@geoand
Copy link
Contributor

geoand commented Apr 2, 2019

You are welcome @sarxos !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants