9

I have 3 groups of tests: unit, integration, acceptance.

  • Latter two groups launches ApplicationContext: minimal for "integration" and full for "acceptance".
  • Both application contexts register queue subscriber.
  • Application contexts are deregistered at the end of entire test run (@RunWith(SpringRunner.class))

When I run "all tests" 2 different application contexts are started and I have duplicate queue subscribers.

I know following workarounds for this subscribers duplication:

  • never run integration and acceptance test together
  • use "acceptance" application context for "integration" tests. Downside: test run will take longer time.
  • add static registry and manually add/remove listeners. Downside: too complex and easy-to-forget

Are there any convinient way to unload application context after a group of tests?

UPDATE based on ndrone answer

  • @DirtiesContext is a perfect match
  • one more option is to limit cached ApplicationContexts count to one with spring.test.context.cache.maxSize=1

Test superclass example with dirties context

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
    @TestExecutionListeners({FlywayTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
    public abstract class AcceptanceTest {}
Sergey Galkin
  • 159
  • 3
  • 8
  • 1
    Have you looked into `@DirtyContext`? http://www.javarticles.com/2016/03/spring-dirtiescontext-annotation-example.html – ndrone Apr 30 '18 at 19:38

0 Answers0