简体   繁体   中英

Can I inject dependencies inside a custom Spring TestExecutionListener?

I think the title is very specific but I'll share some context of what I'm doing.

I'm building an implementation of the TestExecutionListener interface to abstract some common database actions for my tests.

The problem I'm having is that I need to expose an object that is configured inside the listener, to the actual unit tester.

I can either do that by manipulating the context from the TestExecutionListener and add a new singleton bean (which doesn't work).

Or, I can try to move the common object somewhere else, but then I need to inject the locator object to both the unit test and the listener. Autowiring doesn't seem to work there.

Any thoughts or some alternative solution?

I'd better configure this common object as a bean in some context configuration class and then make it available for test classes using @ContextConfiguration(classes = TestConfig.class)

Otherwise, if you really need to configure this object in your TestExecutionListener, you can register this bean anywhere in your context configuration, then autowire it in your TestExecutionListener and change its state here. But it seems to be more a hack than a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM