Running Spring tests in parallel with maven

I have a set of integration tests with SpringJUnit4ClassRunner . I am trying to run them in parallel using maven surefire. However, I noticed that the code is blocked before entering the synchronized block in CacheAwareContextLoaderDelegate.loadContext() .

Is there any way around this cache? I tried to do this, but it looks like there is a more general state than just the cache itself, since my application has stalled inside Spring code. Or can the synchronization be finer-grained, somehow synchronizing on the card key, and not on the entire card?

The motivation for parallel tests is twofold:

  • In some tests, I replace beans with mocks. Since mocks is stateful at its core, I have to create a new ApplicationContext for each test method using @DirtiesContext .
  • In other tests, I want to deploy a subset of Jersey resources. To do this, I define a subset of the Spring configuration classes. Because Spring uses MergedContextConfiguration as the key in the context cache, these tests will not be able to share ApplicationContexts.
+7
source share
2 answers

You might be able to get the best time for your test if you disable parallel test execution. The chapter on testing Spring reference documents has a paragraph on Context Caching :

As soon as the TestContext environment loads the ApplicationContext (or WebApplicationContext) for the test, this context will be cached and reused for all subsequent tests that declare the same unique context configuration in the same test set.

Why is this implemented like this?

This means that the installation cost for loading the application context arises only once (for each set of tests), and subsequent execution of the test is much faster.

How does the cache work?

The Spring TestContext structure stores application contexts in a static cache. This means that the context is literally stored in a static variable. In other words, if the tests are executed in separate processes, the static cache will be cleared between each test execution, and this will effectively disable the caching mechanism.

To take advantage of the caching mechanism, all tests must be performed within the same process or set of tests. This can be achieved by running all tests as a group in the IDE. Similarly, when running tests with an assembly framework such as Ant, Maven, or Gradle, it is important to ensure that the assembly structure does not develop between the tests. For example, if forkMode for the Maven Surefire plugin is always installed or pertest, the TestContext platform will not be able to cache application contexts between test classes, and as a result, the build process will be much slower.

+3
source

One simple thing I could think of is to use @DirtiesContext

0
source

All Articles