2

I use Spock framework to run my tests. Each test class has Spring's annotation @ContextConfiguration over it. As I see context is brought up for every test class and it takes a lot of time to run a couple of dozens of tests in different classes. Is there a way to configure Spock test classes to run under common spring context?

Rostislav V
  • 1,706
  • 1
  • 19
  • 31

2 Answers2

1

It's not about spock but about spring.

Spring can cache in general application context across many test cases but you have to know how to do it right.

As a first resort, make sure, that 'locations' attribute of @ContextConfiguration is the same.

In addition, it's possible to take advantage of @ContextHierarchy annotation.

This caching facility is quite fragile, but works.

Maybe it makes sense to create a common specification with all the annotation and inherit from it:

@ContextConfiguration(locations = ...)
// or maybe
@ContextHierarchy (...)
public abstract class MyCommonSpec extends Specification {
}

// and not in tests:

public class MyTest1 extends MyCommonSpec {
   ...
}

public class MyTest2 extends MyCommonSpec {
   ...
}

Here is a link on a very relevant discussion

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

Possible approach that will work: create multiple classes with tests. For example, A, B, C, D. Make inheritance like: D -> C -> B -> A. Run tests in D, this will execute all the tests from A,B,C,D under common spring context.

I assume that this approach is not so neat and perfect, but it solves initial problem.

Rostislav V
  • 1,706
  • 1
  • 19
  • 31