concept @Shared annotation in category java

appears as: @Shared notation
Java Testing with Spock

This is an excerpt from Manning's book Java Testing with Spock.

4.3.3. Long-lived objects with the @Shared annotation

You can indicate to Spock which objects you want to survive across all test methods by using the @Shared annotation. As an example, assume that you augment your electronic basket with a credit card processor:

Listing 4.22. Using the @Shared annotation

Here you mark the expensive credit card processor with the @Shared annotation. This ensures that Spock creates it only once. On the other hand, the electronic basket itself is lightweight, and therefore it’s created multiple times (once for each test method). Notice also that the credit card processor is closed down once at the end of the test.