Inject members only once when TestInstance.Lifecycle.PER_CLASS #394
Replies: 4 comments
-
I did it like this for simplicity (there are already a lot of logic): such "duplicate" injections assumed to take almost no time. Considering that guice context itself is not re-created, the same dependencies would be autowired each time so it's safe. There is an additional benefit for prototype scoped beans - a new instance of prototype beans would be injected before each test method (could be handy). Of course, its not so hard to add an additional option to change the default behavior, but I'm not sure it worth efforts. I understand that it looks as "not quite correct", but there is no harm and no performance hit (should not be). Most users could even never know about this behaviour. I'm open for discussion. Just to mention:
Yes, extension annotation behaves like that, but you can declare extension in a non-static filed and it would start new application for each test method: @RegisterExtension
TestGuiceyAppExtension ext = TestGuiceyAppExtension.forApp(App.class).create() |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. I just ran all my tests (about ~60 test classes) and measured the time of the "unneeded injections": The total time was almost 43 seconds. I have to say that I have 50 more test classes using some other framework (not |
Beta Was this translation helpful? Give feedback.
-
Oh, 43 seconds is too much. Then I will add this option (keep current behaviour as default). If possible, could you please show an example test (or describe the specifics of your tests): I'm just curious why it takes so much time for injection - I can think of too many injection points, but it could also be the amount of nested tests or something else. |
Beta Was this translation helpful? Give feedback.
-
Thanks! Regarding my tests:
|
Beta Was this translation helpful? Give feedback.
-
Most of my tests (integration) are annotated with
TestInstance.Lifecycle.PER_CLASS
. It works great with my application and feels more natural since the "embedded application" that is being run by theTestGuiceyAppExtension
is also one per the whole class.I am in the process of improving the performance of my tests, and I noticed that the
TestGuiceyAppExtension
injects the members of the test instance before each test method, regardless of theTestInstance.Lifecycle
of the tests class (here).IMO, it makes sense that when the test is annotated with
TestInstance.Lifecycle.PER_CLASS
, the members will be injected once before all the test methods.It would be great if I could somehow configure the test extension so it will behave this way.
Beta Was this translation helpful? Give feedback.
All reactions