I have a test case where I have an @Autowired Foo foo which I need to have injected just once, so all the test methods could re-use the same data foo has. What is currently happening is that upon every @Test method's invocation, the foo bean is cleanly re-loaded by Spring.

Is is possible to load this bean just once for the whole test class and how is it done?

有帮助吗?

解决方案

This behavior is probably environment specific since they differ in the loading of the Spring context. For example, Eclipse does not automatically reload the context between tests but Maven does.

I would suggest that you write a @Before method that copies the @Autowired reference into another field and have your tests use that field.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top