piotrek
piotrek

Reputation: 14520

reuse cached spring contexts to build a bigger context

I have different spring tests. some of them use only entity manager and repositories but some use also other beans and entity manager and repositories. can I reuse the context built for db tests and build just the missing part of bigger context? and therefore avoid expensive build of entity manager again

Upvotes: 1

Views: 169

Answers (1)

Sam Brannen
Sam Brannen

Reputation: 31177

Yes, this is possible via a clever hierarchical structure for your contexts. Beans in any given context can see beans in the same context as well as beans in parent contexts.

You might not actually deploy your production application using such a hierarchy, but creating such a hierarchy in tests would allow you to avoid the performance hit of loading the JPA EntityManager again.

Basically, you'll be using the @ContextHierarchy annotation within a test class hierarchy, where the context loaded for the (potentially abstract) parent test class defines the bean for the EntityManager.

Take a look at the Context hierarchies section of the Testing chapter in the Spring Framework reference manual.

Regards,

Sam (author of the Spring TestContext Framework)

Upvotes: 3

Related Questions