generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Application context fix unreachable code #1639
Merged
luis-pabon-tf
merged 5 commits into
use-inject-for-non-singleton-class
from
application_context_fix_unreachable_code
Dec 5, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ff4781a
Update ApplicationContext.java
luis-pabon-tf e5d184d
Update TestApplicationContext.groovy
luis-pabon-tf 083ddb4
Update TestApplicationContext.groovy
luis-pabon-tf 0ac729e
added unit tests to cover new code
jorg3lopez 18c2f48
Update ApplicationContextTest.groovy
luis-pabon-tf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,9 @@ class TestApplicationContext extends ApplicationContext { | |
TEST_ENV_VARS.clear() | ||
} | ||
|
||
def static injectRegisteredImplementations(def skip = true) { | ||
skipMissingImplementations = skip | ||
doInjectRegisteredImplementations() | ||
def static injectRegisteredImplementations() { | ||
skipMissingImplementations = true | ||
ApplicationContext.injectRegisteredImplementations() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider reintroducing the parameter for skipping missing implementations to maintain flexibility in testing scenarios. [important] |
||
} | ||
|
||
def static addEnvironmentVariable(String key, String value) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I reviewing this too soon?
declaringClassImplementation
can benull
. Looking at thegetDeclaringClassImplementation
method, it can returnnull
whendeclaringClassImplementation == null
andskipMissingImplementations == true
.So, I would keep this if statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can revert this change, though I really could not find a way it would ever hit. This is the word salad I wrote on my notes while trying to make it happen:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. After looking into the code with @luis-pabon-tf, we agreed that this if/else statement could never be null. This is because there is another check before, that checks if the implementing class is in the IMPLEMENTATIONS map. If it is not, then it returns.
In addition, all the classes will have an interface that they are implementing, because they are singleton classes. If they don't, then they wouldn't go through this method, they would go through the non-singleton flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I took another look at this. There is a very small corner case for that if statement. Without the if statement, the following
field.set(declaringClassImplementation, fieldImplementation);
code will throw aNullPointerException
.This corner case is if you do something like the following in a unit test...
Run that unit test and the test fails with a thrown
NullPointerException
from theTestApplicationContext.injectRegisteredImplementations()
line if the if statement is removed. If the if statement was to remain, an exception wouldn't be thrown, and instead the test fails from theinjectedValue == aFieldValue
assertion failing.So, basically to run into this corner case, you need to incorrectly register some implementation as some completely unrelated class. E.g. the
injectionInstantiation
asList
instead ofInjectionDeclaringClass
orAFieldInterface
.I'm comfortable with allowing the removal of the if statement because any unit tests will fail regardless and you have to really mess up registering of implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for hunting down how to trigger that statement. We'd been having a lot of trouble doing so. I am adding your test case example with just a few tweaks into the file.