Kilokahn
Kilokahn

Reputation: 2311

Ability to avoid repeated load of application context in spring TestContext via DependencyInjectionTestExecutionListener

Lets say I have a test class called ServiceTest with three test methods test1, test2 and test3. All three methods use some resources which are provided by spring. In the current state of affairs, if there is a problem with the loading of the spring context, the context load is retried for each test method. Is there a way that I can have it aborted on the first failure itself with existing hook-ins? There might be good reasons behind it - but I fail to appreciate it. Any pointers would be helpful. The real problem is that the context load takes a few minutes and is useless to keep reattempting to load context if it has already failed for the first time and only seeks to prolong the time that the CI engine takes to report failures.

I was thinking of proposing a patch to spring guys that can maintain an attempt map in org.springframework.test.context.TestContext which can be used to track attempts made and refrain from trying over and over again. Thoughts?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:com/kilo/spring/test-context.xml" })
public class ServiceTest {

@Resource(name = "fibonacciService")
private FibonacciService fibonacciService;

@Test
public void test1() {
    long fibonacci = fibonacciService.getFibonacci(5);
}

@Test
public void test2() {
    long fibonacci = fibonacciService.getFibonacci(4);
}

@Test
public void test3() {
    long fibonacci = fibonacciService.getFibonacci(6);
}

Upvotes: 4

Views: 4907

Answers (2)

Sam Brannen
Sam Brannen

Reputation: 31197

(This answer supersedes my previous, outdated answer)

I just implemented built-in support for a "test context failure threshold" in Spring Framework 6.1 M1.

As of Spring Framework 6.1, a context failure threshold policy is in place which helps avoid repeated attempts to load a failing ApplicationContext. By default, the failure threshold is set to 1 which means that only one attempt will be made to load an ApplicationContext for a given context cache key. Any subsequent attempt to load the ApplicationContext for the same context cache key will result in an immediate IllegalStateException with an error message which explains that the attempt was preemptively skipped.

For further details, consult the Spring Framework reference manual.

Upvotes: 1

Sam Brannen
Sam Brannen

Reputation: 31197

No, there is currently no way to have ApplicationContext loading aborted on the first failure with existing hook-ins.

Feel free to create a JIRA issue against the SPR project and the Test component with your suggestions.

Regards,

Sam (author of the Spring TestContext Framework)

Upvotes: 3

Related Questions