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

<newbie> Handling dependency injection #5142

Closed
yatesco opened this issue May 25, 2022 · 4 comments
Closed

<newbie> Handling dependency injection #5142

yatesco opened this issue May 25, 2022 · 4 comments

Comments

@yatesco
Copy link

yatesco commented May 25, 2022

Hi - I think I've followed the docs correctly but I can't get this to work, and it's such a simple use case that I must be missing something.

Quite simply, how do I tell the framework that something is always non null using field injection, without having to wrap each and every access with if (!this.collaborator != null)?

I have:

    @Inject
    private AgroalDataSource defaultDataSource;

    @Test
    public void testDataJdbc() throws SQLException {
        try (var connection = this.defaultDataSource.getConnection()) {
...
        }
    }

The only way I can get this to work is wrapping the reference to this.defaultDataSource in a if (!null...):

    public void testDataJdbc() throws SQLException {
        if (this.defaultDataSource != null) {
            try (var connection = this.defaultDataSource.getConnection()) {
...
            }
        }
    }

I can't use constructor injection, which does work, because:

  • it doesn't work for the Entity Persistence logic
  • Some of Quarkus' integration testing framework expects a zero arg constructor

Things I've tried:

  • hoping the framework would recognise and magically handle javax.inject.Inject; :-).
  • annotating the field with @MonotonicNonNull and then a @SetUp method which asserts it isn't null
  • using @MonotonicNonNull and adding an assert this.defaultDataSource != null as the first line in testDataJdbc
  • randomly copying in @SuppressWarnings("initialization.fields.uninitialized") from a StackOverflow post. Hey - we've all done it.

I'm confused. What's the correct approach here? Thanks!

@smillst
Copy link
Member

smillst commented May 25, 2022

From the code snippet you provided, I'm assuming you are getting an initialization.fields.uninitialized warning. This is expected because the framework doesn't that the field is initialized. The correct approach would be to suppress that warning with a comment that the field is initialized via dependency injection.

Fields are @NonNull by default, so you should not need to check if this.defaultDataSource is null before using it. If you are getting a dereference.of.nullable error, I can't see why that would be happening given the code you've provided.

@yatesco
Copy link
Author

yatesco commented May 25, 2022

thanks @smillst. I did add @SuppressWarnings("initialization.fields.uninitialized") to the field but it didn't take any effect.

@yatesco
Copy link
Author

yatesco commented May 25, 2022

ah - according to https://checkerframework.org/manual/#initialization-checking-suppressing-warnings it is @SuppressWarnings("initialization"). Closing now.

@yatesco yatesco closed this as completed May 25, 2022
@smillst
Copy link
Member

smillst commented May 25, 2022

@SuppressWarnings should be added to place the error is issued. In this case, it's probably a constructor or if the class has not constructor, then it should be applied to the class. Also, in the case of no constructor, the warning key is slightly different: initialization.field.uninitialized. (No s at the end of field.)

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

2 participants